這篇文章主要介紹了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);
以上是HttpPostedFileBase檔案上傳實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),