예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="postedFile"></param>
        /// <returns></returns>
        public static FileEntity CreateFileEntity(HttpPostedFileBase postedFile, string uploadDir = "Uploads", string strDir = "")
        {
            //Random random = new Random(unchecked((int)DateTime.Now.Ticks));
            string fileName    = Path.GetFileName(postedFile.FileName);
            string fileExtName = Path.GetExtension(postedFile.FileName);

            if (!string.IsNullOrEmpty(fileExtName) && fileExtName.Length > 1)
            {
                fileExtName = fileExtName.Substring(1);
            }
            string fileType = postedFile.ContentType.ToLower();
            int    fileSize = postedFile.ContentLength;
            //string newFileName = string.Format("{0}{1}.{2}",
            //                  (Environment.TickCount & int.MaxValue).ToString(),
            //                  random.Next(1000, 9999).ToString(),
            //                  fileExtName);
            string newFileName = string.Format("{0}.{1}",
                                               Guid.NewGuid().ToString("n"),
                                               fileExtName);

            if (fileType == "application/octet-stream")
            {
                fileType = GetContentType(fileExtName);
            }

            string saveDir = GetSavePathByDate();

            if (!string.IsNullOrEmpty(strDir))
            {
                saveDir = strDir + "/" + saveDir;
            }
            FileEntity entity = new FileEntity();

            entity.Name       = newFileName;
            entity.ExtName    = fileExtName;
            entity.Path       = saveDir.Replace(Path.DirectorySeparatorChar, '/') + newFileName;
            entity.Size       = fileSize;
            entity.Type       = fileType;
            entity.SourceName = fileName;
            return(entity);
        }
예제 #2
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="postedFile"></param>
 /// <param name="uploadDir"></param>
 /// <param name="strDir"></param>
 /// <param name="logger"></param>
 /// <param name="serverUtility"></param>
 /// <returns></returns>
 public static async Task <FileEntity> UploadFileAsync(HttpPostedFileBase postedFile, string uploadDir, string strDir = "")
 {
     try
     {
         FileEntity entity     = CreateFileEntity(postedFile, uploadDir, strDir);
         string     uploadPath = uploadDir;
         if (string.IsNullOrEmpty(uploadPath))
         {
             uploadPath = "Uploads";
         }
         var saveDir = entity.Path.Substring(0, entity.Path.LastIndexOf('/'));
         uploadPath = GetMapPath(uploadDir);
         uploadPath = uploadPath.TrimEnd('\\') + "\\" + saveDir;
         if (!Directory.Exists(uploadPath))
         {
             Directory.CreateDirectory(uploadPath);
         }
         string filePath = uploadPath + Path.DirectorySeparatorChar + entity.Name;
         using (var fs = new FileStream(
                    path: filePath,
                    mode: FileMode.Create,
                    access: FileAccess.Write,
                    share: FileShare.None,
                    bufferSize: BUFFER_SIZE,
                    useAsync: true))
         {
             var buffer = new byte[BUFFER_SIZE];
             int numRead;
             while ((numRead = await postedFile.InputStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
             {
                 await fs.WriteAsync(buffer, 0, buffer.Length);
             }
         }
         return(entity);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }