コード例 #1
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            CurrentLocation = ConfigurationManager.AppSettings["RootPath"];
            // The file Extension
            string fileExtension = string.Empty;

            // The choosed file name
            string fileName = string.Empty;

            // The new file name in the server
            string newFileName = string.Empty;

            // Check if choosed a file
            if (!fuChooseFile.HasFile)
            {
                lbMessage.Text = "Please choose the file you want to upload. " +
                                 "Note: The file size cannot be zero.";
                return;
            }


            fileExtension = Path.GetExtension(fuChooseFile.FileName);
            fileName      = string.Format("{0}\\{1}", CurrentLocation,
                                          (string.IsNullOrEmpty(fileExtension) ? fuChooseFile.FileName :
                                           fuChooseFile.FileName.Replace(fileExtension, "")));

            if (fileExtension.ToLower() == ".exe" || fileExtension.ToLower() == ".msi")
            {
                lbMessage.Text =
                    "The file you want to upload cannot be a .exe or .msi file.";
                return;
            }

            newFileName = fileName;

            // Check file size
            //if (fuChooseFile.PostedFile.ContentLength >= 1024*1024*40)
            //{
            //    lbMessage.Text =
            //        "The file you want to upload cannot be larger than 40 MB.";
            //    return;
            //}

            try
            {
                // If there is already a file with the same name in the system,rename
                // and then upload the file .
                int i = 0;
                while (File.Exists(newFileName + fileExtension))
                {
                    i++;
                    newFileName = string.Format(fileName + "({0})", i);
                }

                fuChooseFile.SaveAs(Utilities.FixRoot(newFileName + fileExtension));

                lbMessage.Text =
                    string.Format("The file \"{0}\" was uploaded successfully!",
                                  Path.GetFileName(fileName));

                ShowFolderItems();
            }
            catch (HttpException he)
            {
                lbMessage.Text =
                    string.Format("File {0} upload failed because of the following error:{1}.",
                                  fuChooseFile.PostedFile.FileName, he.Message);
            }

            //the file id
            var _file2 = new FileInfo(newFileName);
            var db     = new CloudAppDbEntities();
            var _file  = new UserFile()
            {
                FName      = Path.GetFileName(newFileName),
                FileName   = Path.GetFileName(fileName),
                UploaderID = currentUserID,
                // don't forget to find currentuserID
                //FileSize = CommonUse.FormatFileSize((long)fuChooseFile.PostedFile.ContentLength) ,
                FileSize   = CommonUse.FormatFileSize((long)fuChooseFile.PostedFile.ContentLength),
                FileType   = fileExtension,
                UploadTime = _file2.CreationTime,
                CourseID   = null,
                ClassID    = classID
            };

            db.UserFile.Add(_file);
            db.SaveChanges();
        }