ASP.NET MVC實現(xiàn)批量文件上傳
根據(jù)項目需要,研究了一下如何在ASP.NETMVC下實現(xiàn)批量文件上傳。首先,介紹單文件上傳;然后,介紹多文件上傳如何實現(xiàn)。
一、單文件上傳
單文件上傳的原理是將文件數(shù)據(jù)放入request中,由頁面直接傳遞至后臺controller中,類似于view和controller之間傳參數(shù),直接貼上代碼加注釋。
Upload.aspx文件中的代碼:
<form enctype="multipart/form-data" method="post"> <input type="file" id="file" /> <input type="submit" value="上傳" /> </form>
Controller中代碼:
[HttpPost] public ActionResult Upload(FormCollection form) { if (Request.Files.Count == 0){ //Request.Files.Count 文件數(shù)為0上傳不成功 return View(); } var file = Request.Files[0]; if (file.ContentLength == 0){ //文件大小大(以字節(jié)為單位)為0時,做一些操作 return View(); } else{ //文件大小不為0 file = Request.Files[0]; //服務(wù)器上的UpLoadFile文件夾必須有讀寫權(quán)限 string target = Server.MapPath("/")+("/Mock/Learning/");//取得目標(biāo)文件夾的路徑 string filename = file.FileName;//取得文件名字 string path = target + filename;//獲取存儲的目標(biāo)地址 file.SaveAs(path);} return View(); }
這里需要注意的是,在ASP.NET中,request的默認(rèn)大小為4M,因此,如需上傳較大文件,需要更改Web.config。
<system.web> <httpRuntime maxRequestLength="40960"/> </system.web>
二、批量文件上傳
思路是通過js根據(jù)用戶需求動態(tài)添加上傳控件,多個文件通過request一并上傳至controller。
Upload.aspx文件中的代碼:
<form enctype="multipart/form-data" method="post"> <div id="FileList"> <div> <input type="file" id="file" name="file0"/> </div> </div> <p> <a onclick="AddFile();">添加文件</a> </p> <p> <input type="submit" value="上傳" /> </p> </form> <script> var index = 1; function AddFile() { var ul = document.getElementById("FileList"); var inputDiv = document.createElement("div"); inputDiv.setAttribute("Id", "div" + index); var file = document.createElement("input"); file.setAttribute("type", "file"); file.setAttribute("id", "file" + index); file.setAttribute("name", "file" + index); var btnDel = document.createElement("input"); btnDel.setAttribute("type", "button"); btnDel.setAttribute("value", "刪除"); btnDel.setAttribute("Id", index); btnDel.onclick = function() { inputDiv.removeChild(file); inputDiv.removeChild(btnDel); ul.removeChild(inputDiv); } inputDiv.appendChild(file); inputDiv.appendChild(btnDel); ul.appendChild(inputDiv); index++; } </script>
Controller中的代碼:
[HttpPost] public ActionResult Upload(FormCollection form) { foreach (string item in Request.Files) { HttpPostedFileBase file = Request.Files[item] as HttpPostedFileBase; if (file==null || file.ContentLength == 0) continue; //判斷Upload文件夾是否存在,不存在就創(chuàng)建 string path = Server.MapPath("..//Upload"); if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } path = AppDomain.CurrentDomain.BaseDirectory + "Upload/"; //獲取上傳的文件名 string fileName = file.FileName; //上傳 file.SaveAs(Path.Combine(path,fileName)); } return Content("<script>alert('上傳文件成功');window.history.back();</script>"); }
注意在Request.Files中,不同文件的index是上傳控件的name屬性值,因此在aspx頁面代碼中必須保證多個上傳控件的name屬性值互不相同。
以上便實現(xiàn)了批量文件上傳。
本人才疏學(xué)淺,僅供大家參考,若有不當(dāng)之處,請大家批評指正!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC HttpPostedFileBase文件上傳的實例代碼
- asp.net core mvc實現(xiàn)文件上傳實例
- asp.net mvc 實現(xiàn)文件上傳帶進(jìn)度條的思路與方法
- 解決ASP.NET Core Mvc文件上傳限制問題實例
- asp.net中MVC借助Iframe實現(xiàn)無刷新上傳文件實例
- Asp.net實現(xiàn)MVC處理文件的上傳下載功能實例教程
- ASP.NET MVC處理文件上傳的小例子
- 用Html5與Asp.net MVC上傳多個文件的實現(xiàn)代碼
- ASP.NET?MVC使用JSAjaxFileUploader插件實現(xiàn)單文件上傳
相關(guān)文章
使用Supervisor守護(hù)ASP.NET?Core應(yīng)用程序進(jìn)程
這篇文章介紹了使用Supervisor守護(hù)ASP.NET?Core應(yīng)用程序進(jìn)程的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03Asp.net Core 1.1 升級后操作mysql出錯的解決辦法
這篇文章主要介紹了Asp.net Core 1.1 升級后操作mysql出錯的解決辦法,需要的朋友可以參考下2016-12-12在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)
可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件來實現(xiàn)表單或者URL提交數(shù)據(jù)的獲取,獲取后傳給SQLInjectionHelper類ValidUrlData方法來完成檢查2013-01-01asp.net得到本機數(shù)據(jù)庫實例的兩種方法代碼
這篇文章介紹了asp.net得到本機數(shù)據(jù)庫實例的兩種方法代碼,有需要的朋友可以參考一下2013-07-07