ASP.NET MVC 使用表单上传文件
开发项目中经常会用到上传文件功能,之前在做 WebForm 开发的时候写过上传功能都是基于 Handlers 的,最近在弄一个上传身份证的功能想通过 MVC 的表单直接提交到后台,所以研究了下文件上传,下面是我实际应用中的使用方法,供大家参考。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | HttpFileCollectionBase files = Request.Files; if (files.Count != 0 && files.AllKeys.Count() == 2) { string fileName = DateTime.Now.ToString( "yyyyMMddhhmmss" ); if (files[ "IdCardFacePic" ] != null ) { var picIdCardFace = files[ "IdCardFacePic" ]; string expName = Path.GetFileName(picIdCardFace.FileName).Split( '.' ).LastOrDefault(); var picIdCardFacePath = Path.Combine(Request.MapPath( string .Format( "~/{0}{1}" , ImagesUpload, IdCardUpload)), "IdCardFace_" + fileName + "." + expName); try { picIdCardFace.SaveAs(picIdCardFacePath); model.IdCardFacePicUrl = Path.Combine( string .Format( "{0}{1}" , ImagesUpload, IdCardUpload), "IdCardFace_" + fileName + "." + expName); } catch { //return Content("上传异常 !", "text/plain"); } } if (files[ "IdCardBackPic" ] != null ) { var picIdCardBack = files[ "IdCardBackPic" ]; string expName = Path.GetFileName(picIdCardBack.FileName).Split( '.' ).LastOrDefault(); var picIdCardBackPath = Path.Combine(Request.MapPath( string .Format( "~/{0}{1}" , ImagesUpload, IdCardUpload)), "IdCardBack_" + fileName + "." + expName); try { picIdCardBack.SaveAs(picIdCardBackPath); model.IdCardBackPicUrl = Path.Combine( string .Format( "{0}{1}" , ImagesUpload, IdCardUpload), "IdCardBack_" + fileName + "." + expName); } catch { //return Content("上传异常 !", "text/plain"); } } } |
前端 View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | @ using (Html.BeginForm( "Edit" , "User" , FormMethod.Post, new { @id = "submitForm" , enctype = "multipart/form-data" })) { @Html.HiddenFor(m => m.Id) @*@Html.HiddenFor(m=>m.us)*@ <div class = "page27-form" > <div class = "f-form01-item" > <label class = "f-form01-label" >姓名</label> <div class = "f-form01-main" > @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @ class = "f-form01-input100" , @id = "policyHolderName" } }) @Html.ValidationMessageFor(model => model.Name, "" , new { @ class = "f-form01-input100" }) </div> </div> <div class = "f-form01-item" id= "divIdCardBox" > <label class = "f-form01-label" >证件号码</label> <div class = "f-form01-main" > @Html.EditorFor(model => model.IdCard, new { htmlAttributes = new { @ class = "f-form01-input100" , @id = "policyHolderIdCardNumber" } }) @Html.ValidationMessageFor(model => model.IdCard, "" , new { @ class = "text-danger" }) </div> </div> </div> <div class = "page27-sfz" id= "divIdCardPicBox" > <div class = "f-form01-item" > @ if ( string .IsNullOrEmpty(Model.IdCardFacePicUrl)) { <div class = "f-form01-main" > 上传身份证正面 </div> } else { <div class = "f-form01-main" > <img src= "@Url.Content(Model.IdCardFacePicUrl)" width= "100px" height= "50px" alt= "img" /> </div> } <div class = "f-form01-other" > <a href= "#" id= "btnUploadFacePic" class = "f-button02" >上传正面</a> <input class = "f-button02" name= "IdCardFacePic" id= "IdCardFacePicUrl" type= "file" style= "display:none" autocomplete= "off" disableautocomplete= "" > </div> </div> <div class = "f-form01-item" > @ if ( string .IsNullOrEmpty(Model.IdCardBackPicUrl)) { <div class = "f-form01-main" > 上传身份证反面 </div> } else { <div class = "f-form01-main" > <img src= "@Url.Content(Model.IdCardBackPicUrl)" width= "100px" height= "50px" alt= "img" /> </div> } <div class = "f-form01-other" > <a href= "#" id= "btnUploadBackPic" class = "f-button02" >上传反面</a> <input class = "f-button02" name= "IdCardBackPic" id= "IdCardBackPicUrl" type= "file" style= "display:none" autocomplete= "off" disableautocomplete= "" > </div> </div> </div> <input class = "f-button01" type= "submit" id= "btnSubmit" style= "margin-top: 1rem;" value= "保存" > } |
需要注意的是 Form 里要加上 enctype 属性,不然后台无法接收到上传的图片文件。
enctype = "multipart/form-data"
最后上一张效果图。
分类:
DotNet
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架