公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C# .NET MVC實現圖片、視頻上傳接口

admin
2025年1月3日 10:5 本文熱度 4123
在程序開發項目中我們經常會上傳圖片或者視頻,下面介紹一下用.NET MVC實現圖片、視頻上傳接口的三種方式;有興趣的小伙伴們可以一起學習。

一、 Base64圖片上傳

適合小程序端

/// </summary>/// <param name="base64String"></param>/// <param name="filePath"></param>/// <param name="fileName"></param>/// <returns></returns>public static string UpLoadImage(string base64String){	FileStream fs = null;	BinaryWriter bw = null;	try	{		if (string.IsNullOrEmpty(base64String))		{			throw new Exception("圖片參數不能為空");		}		string filePath = "/UpLoad/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;		string realPath = HttpContext.Current.Server.MapPath("~/" + filePath);		string day = YearMonthDay;		if (!Directory.Exists(realPath))//如果不存在就創建file文件夾		{			Directory.CreateDirectory(realPath);		}
string imagePath = filePath + "/" + fileName; string imageSavePath = realPath + "/" + fileName; byte[] arr = Convert.FromBase64String(base64String); fs = new System.IO.FileStream(imageSavePath, System.IO.FileMode.Create); bw = new System.IO.BinaryWriter(fs); bw.Write(arr); bw.Close(); fs.Close();
return imagePath; } catch (Exception ex) {
if (bw != null) { bw.Close(); } if (fs != null) { fs.Close(); } throw new Exception(ex.Message); }}

二、HttpPostedFile文件上傳

適合網頁端

/// <summary>/// 上傳圖片、視頻 HttpPostedFile/// </summary>/// <returns></returns>[HttpPost]public ApiResponse UploadPostFile(){	try	{		string filePath = "/UpLoad/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;		string realPath = HttpContext.Current.Server.MapPath("~/" + filePath);		if (!Directory.Exists(realPath))//如果不存在就創建file文件夾		{			Directory.CreateDirectory(realPath);		}
HttpPostedFile file = HttpContext.Current.Request.Files[0]; string fileType = HttpContext.Current.Request.Form["fileType"];
string imagePath = filePath + "/" + "test.jpg";
file.SaveAs(imagePath);
return imagePath; } catch (Exception e) { return BaseApiResponse.ApiError(e.Message); }}

三、FormData上傳(通過表單(multipart/form-data)的方式上傳后)

安裝MultipartDataMediaFormatter

?

路由配置

配置代碼

//為了實現通過實體類獲取文件GlobalConfiguration.Configuration.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());

file和表單一起

/// <param name="formData"></param>/// <returns></returns>[HttpPost]public string UploadFormData(FormData formData){	try	{		string filePath = "/UpLoad/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
string realPath = HttpContext.Current.Server.MapPath("~/" + filePath); if (!Directory.Exists(realPath))//如果不存在就創建file文件夾 { Directory.CreateDirectory(realPath); }
string fileType = formData.Fields[0].Value;
string imagePath = filePath + "/" + "test.jpg";
FileStream fs = null; BinaryWriter bw = null; byte[] arr = formData.Files[0].Value.Buffer; fs = new System.IO.FileStream(imagePath, System.IO.FileMode.Create); bw = new System.IO.BinaryWriter(fs); bw.Write(arr); bw.Close(); fs.Close();
return imagePath; } catch (Exception e) { throw new Exception(ex.Message); }}

四、用Postman測試一下吧

成功接收到圖片

這就完成了,快去試試吧。


該文章在 2025/1/3 16:54:23 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 成人精品午夜在线观看 | 国产一区二区三区韩国女主播 | 国产高清午夜自 | 国产黄片一区二区 | 国产高清在线新超碰在线播放 | 91精品国产福利在线导航 | 国产免费露脸视频在线 | 国产精品一区波多野结衣 | 波多野结衣日本一区二区在线观 | 国产午夜精品喷水久久 | 精品国产无码一区二区 | 国产精品一区二区久久不卡 | av在线播放免费 | 国产精品亚洲精品日韩动图 | 国产成人综合在线观看网站 | 国产精品国产 | 国产精品毛片a∨一区二区三 | 国产一区在线观看无码av | 国产av无码片毛片一级 | 国产成人啪精品午夜网站 | 国产午夜精品夜夜骚久久久久 | 国产欧美日韩在线一区二区不卡 | 国产成人精品免费视频大全 | 国产成人av丝袜在线播放 | 国产日韩精品亚欧免费视频 | 成人精品永久免费视频 | 国产做国产爱免费视频 | 国产黑丝袜在线观看 | 成片在线看一区二区草莓 | 国产福利小电影视福利在线 | 精品无码国产一区二区一区51安 | 2025国产精产品在线不卡 | 国产午夜福利在线看 | 99精品国产一区二区三区 | 91大片淫黄大片在线天堂 | 国产成人免费无庶挡视频在线观看 | 国产真实露脸多 | 国产人妖熟女 | 国产精品无码一区二区在线 | 国产无码一区二区三区在线观看 | 国产成人精品麻豆免费网站 |