這篇文章主要介紹了ASP.NET MVC HttpPostedFileBase檔案上傳,具有一定的參考價值,感興趣的小夥伴們可以參考一下
本文介紹了ASP.NET MVC HttpPostedFileBase檔案上傳 ,分享給大家,希望對大家有協助
HttpPostedFileBase檔案上傳,支援多檔案一次上傳,如有圖片,則支援略縮圖儲存
檔案傳輸資訊封裝
/// <summary> /// 檔案產生方式 /// </summary> public class UpFileMessage { /// <summary> /// 檔案名稱 /// </summary> public string OriginalFileName { get; set; } /// <summary> /// 是否儲存略縮圖 /// </summary> public bool IsImage { get; set; } /// <summary> /// 檔案流 /// </summary> public Stream FileStream { get; set; } /// <summary> /// 產生縮圖的方式 /// [WH]-指定寬高 /// [H]-指定高,按比例縮放寬 /// [W]-指定寬,按比例縮放高 /// </summary> public string Mode { get; set; } /// <summary> /// 略縮圖高度 /// </summary> public int? ThumbHeight { get; set; } /// <summary> /// 略縮圖寬度 /// </summary> public int? ThumbWidth { get; set; } }
檔案上傳返回結果
/// <summary> /// 檔案上傳返回結果 /// </summary> public class UpFileResultMessage { /// <summary> /// 檔案儲存是否成功 /// </summary> public bool IsSuccess { get; set; } /// <summary> /// 錯誤訊息 /// </summary> public string Message { get; set; } /// <summary> /// 原始檔案名-(無副檔名) /// </summary> public string FileName { get; set; } /// <summary> /// 檔案名稱副檔名 /// </summary> public string FileSuffix { get; set; } /// <summary> /// 檔案名稱儲存路徑 /// </summary> public string FilePath { get; set; } /// <summary> /// 檔案類型為圖片時 /// 縮圖儲存路徑 /// </summary> public string ThumbPath { get; set; } /// <summary> /// 儲存檔案名稱(無副檔名) /// </summary> public string SaveFileName { get; set; } /// <summary> /// 檔案自增ID /// </summary> public int[] FileIdArray { get; set; } }
檔案上傳類庫
需引用System.Web命名空間,並對 [System.Web.UI.Page] 進行繼承,調用Server.MapPath方法
public class FileUtil : System.Web.UI.Page { /// <summary> /// 圖片上傳 /// </summary> /// <param name="fileMessage">檔案產生方式</param> /// <returns></returns> public UpFileResultMessage UpLoadFile(UpFileMessage fileMessage) { try { string now = DateTime.Today.ToString("yyyyMMdd"); string guid = Guid.NewGuid().ToString(); //擷取副檔名 var fileSuffix = Path.GetExtension(fileMessage.OriginalFileName); var uploadFolder = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(ParmsConfig.UpFilePathUrl), now); if (!Directory.Exists(uploadFolder)) { Directory.CreateDirectory(uploadFolder); } //儲存檔案名稱 string saveFileName = guid + fileSuffix; string filePath = Path.Combine(uploadFolder, saveFileName); UpFileResultMessage upFileResult = new UpFileResultMessage() { IsSuccess = true, FileName = Path.GetFileNameWithoutExtension(fileMessage.OriginalFileName), FileSuffix = fileSuffix, FilePath = string.Format(@"{0}/{1}", ParmsConfig.UpFileIPAddressUrl, now), SaveFileName = guid }; using (Stream sourceStream = fileMessage.FileStream) { using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { const int bufferLen = 1024 * 4;//4KB byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) { targetStream.Write(buffer, 0, count); } } //上傳檔案為圖片時,需建立縮圖 if (fileMessage.IsImage) { var uploadThumbFolder = Path.Combine(uploadFolder, "Thumb"); if (!Directory.Exists(uploadThumbFolder)) { Directory.CreateDirectory(uploadThumbFolder); } using (FileStream targetStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) { System.Drawing.Image image = System.Drawing.Image.FromStream(targetStream); int width = image.Width; int height = image.Height; int thumbWidth = 0; int thumbHeight = 0; switch (fileMessage.Mode) { case "WH": //指定高寬縮放(可能變形) thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200; thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200; break; case "W": //指定寬,高按比例 thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200; thumbHeight = height * thumbWidth / width; break; case "H": //指定高,寬按比例 thumbHeight = fileMessage.ThumbHeight.HasValue ? fileMessage.ThumbHeight.Value : 200; thumbWidth = width * thumbHeight / height; break; default: thumbWidth = fileMessage.ThumbWidth.HasValue ? fileMessage.ThumbWidth.Value : 200; thumbHeight = height * thumbWidth / width; break; } string thumbFilePath = Path.Combine(uploadThumbFolder, saveFileName); CreateThumbnail(thumbFilePath, targetStream, thumbWidth, thumbHeight); upFileResult.ThumbPath = string.Format(@"{0}/{1}/Thumb", ParmsConfig.UpFileIPAddressUrl, now); } } } return upFileResult; } catch (Exception ex) { return new UpFileResultMessage() { IsSuccess = false, Message = ex.Message }; } } /// <summary> /// 建立指定圖片檔案流的縮圖 /// </summary> /// <param name="thumbFilePath">縮圖檔案儲存路徑</param> /// <param name="fileStream">原始檔案流</param> /// <param name="width">縮圖寬</param> /// <param name="height">縮圖高</param> private void CreateThumbnail(string thumbFilePath, Stream fileStream, int width, int height) { using (Image image = Image.FromStream(fileStream)) { using (Image thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero)) { thumbnail.Save(thumbFilePath); } } } }
調用方式
var upFileMsg = new UpFileMessage() { IsImage = true, OriginalFileName = platformImg[i].FileName, FileStream = platformImg[i].InputStream, ThumbWidth = ThumbWidth, Mode = "W" }; var upFileResultMsg = new FileUtil().UpLoadFile(upFileMsg);