HttpPostedFileBase文件上傳,支持多文件一次上傳,若有圖片,則支持略縮圖保存ide
文件傳輸信息封裝ui
1 /// <summary> 2 /// 文件生成方式 3 /// </summary> 4 public class UpFileMessage 5 { 6 /// <summary> 7 /// 文件名 8 /// </summary> 9 public string OriginalFileName { get; set; } 10 11 /// <summary> 12 /// 是否保存略縮圖 13 /// </summary> 14 public bool IsImage { get; set; } 15 16 /// <summary> 17 /// 文件流 18 /// </summary> 19 public Stream FileStream { get; set; } 20 21 /// <summary> 22 /// 生成縮略圖的方式 23 /// [WH]-指定寬高 24 /// [H]-指定高,按比例縮放寬 25 /// [W]-指定寬,按比例縮放高 26 /// </summary> 27 public string Mode { get; set; } 28 29 /// <summary> 30 /// 略縮圖高度 31 /// </summary> 32 public int? ThumbHeight { get; set; } 33 34 /// <summary> 35 /// 略縮圖寬度 36 /// </summary> 37 public int? ThumbWidth { get; set; } 38 39 }
文件上傳返回結果spa
1 /// <summary> 2 /// 文件上傳返回結果 3 /// </summary> 4 public class UpFileResultMessage 5 { 6 /// <summary> 7 /// 文件保存是否成功 8 /// </summary> 9 public bool IsSuccess { get; set; } 10 11 /// <summary> 12 /// 錯誤消息 13 /// </summary> 14 public string Message { get; set; } 15 16 /// <summary> 17 /// 原始文件名-(無擴展名) 18 /// </summary> 19 public string FileName { get; set; } 20 21 /// <summary> 22 /// 文件名擴展名 23 /// </summary> 24 public string FileSuffix { get; set; } 25 26 /// <summary> 27 /// 文件名保存路徑 28 /// </summary> 29 public string FilePath { get; set; } 30 31 /// <summary> 32 /// 文件類型爲圖片時 33 /// 縮略圖保存路徑 34 /// </summary> 35 public string ThumbPath { get; set; } 36 37 /// <summary> 38 /// 保存文件名(無擴展名) 39 /// </summary> 40 public string SaveFileName { get; set; } 41 42 /// <summary> 43 /// 文件自增ID 44 /// </summary> 45 public int[] FileIdArray { get; set; } 46 }
文件上傳類庫code
需引用System.Web命名空間,並對 [System.Web.UI.Page] 進行繼承,調用Server.MapPath方法orm
1 public class FileUtil : System.Web.UI.Page 2 { 3 /// <summary> 4 /// 圖片上傳 5 /// </summary> 6 /// <param name="fileMessage">文件生成方式</param> 7 /// <returns></returns> 8 public UpFileResultMessage UpLoadFile(UpFileMessage fileMessage) 9 { 10 try 11 { 12 string now = DateTime.Today.ToString("yyyyMMdd"); 13 string guid = Guid.NewGuid().ToString(); 14 15 //獲取文件擴展名 16 var fileSuffix = Path.GetExtension(fileMessage.OriginalFileName); 17 18 var uploadFolder = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(ParmsConfig.UpFilePathUrl), now); 19 20 if (!Directory.Exists(uploadFolder)) 21 { 22 Directory.CreateDirectory(uploadFolder); 23 } 24 25 //保存文件名 26 string saveFileName = guid + fileSuffix; 27 string filePath = Path.Combine(uploadFolder, saveFileName); 28 29 30 UpFileResultMessage upFileResult = new UpFileResultMessage() 31 { 32 IsSuccess = true, 33 FileName = Path.GetFileNameWithoutExtension(fileMessage.OriginalFileName), 34 FileSuffix = fileSuffix, 35 FilePath = string.Format(@"{0}/{1}", ParmsConfig.UpFileIPAddressUrl, now), 36 SaveFileName = guid 37 }; 38 39 using (Stream sourceStream = fileMessage.FileStream) 40 { 41 using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) 42 { 43 const int bufferLen = 1024 * 4;//4KB 44 byte[] buffer = new byte[bufferLen]; 45 int count = 0; 46 while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) 47 { 48 targetStream.Write(buffer, 0, count); 49 } 50 } 51 //上傳文件爲圖片時,需建立縮略圖 52 if (fileMessage.IsImage) 53 { 54 var uploadThumbFolder = Path.Combine(uploadFolder, "Thumb"); 55 56 if (!Directory.Exists(uploadThumbFolder)) 57 { 58 Directory.CreateDirectory(uploadThumbFolder); 59 } 60 using (FileStream targetStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) 61 { 62 System.Drawing.Image image = System.Drawing.Image.FromStream(targetStream); 63 int width = image.Width; 64 int height = image.Height; 65 int thumbWidth = 0; 66 int thumbHeight = 0; 67 switch (fileMessage.Mode) 68 { 69 case "WH": //指定高寬縮放(可能變形) 70 thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200; 71 thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200; 72 break; 73 case "W": //指定寬,高按比例 74 thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200; 75 thumbHeight = height * thumbWidth / width; 76 break; 77 case "H": //指定高,寬按比例 78 thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200; 79 thumbWidth = width * thumbHeight / height; 80 break; 81 default: 82 thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200; 83 thumbHeight = height * thumbWidth / width; 84 break; 85 } 86 string thumbFilePath = Path.Combine(uploadThumbFolder, saveFileName); 87 CreateThumbnail(thumbFilePath, targetStream, thumbWidth, thumbHeight); 88 upFileResult.ThumbPath = string.Format(@"{0}/{1}/Thumb", ParmsConfig.UpFileIPAddressUrl, now); 89 } 90 } 91 } 92 93 return upFileResult; 94 } 95 catch (Exception ex) 96 { 97 return new UpFileResultMessage() { IsSuccess = false, Message = ex.Message }; 98 } 99 100 } 101 102 /// <summary> 103 /// 建立指定圖片文件流的縮略圖 104 /// </summary> 105 /// <param name="thumbFilePath">縮略圖文件保存路徑</param> 106 /// <param name="fileStream">原始文件流</param> 107 /// <param name="width">縮略圖寬</param> 108 /// <param name="height">縮略圖高</param> 109 private void CreateThumbnail(string thumbFilePath, Stream fileStream, int width, int height) 110 { 111 using (Image image = Image.FromStream(fileStream)) 112 { 113 using (Image thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero)) 114 { 115 thumbnail.Save(thumbFilePath); 116 } 117 } 118 119 } 120 121 }
調用方式blog
1 var upFileMsg = new UpFileMessage() 2 { 3 IsImage = true, 4 OriginalFileName = platformImg[i].FileName, 5 FileStream = platformImg[i].InputStream, 6 ThumbWidth = ThumbWidth, 7 Mode = "W" 8 }; 9 var upFileResultMsg = new FileUtil().UpLoadFile(upFileMsg);
代碼地址:文件上傳類庫包.zip繼承