ASP.NET MVC實(shí)現(xiàn)批量文件上傳
根據(jù)項(xiàng)目需要,研究了一下如何在ASP.NETMVC下實(shí)現(xiàn)批量文件上傳。首先,介紹單文件上傳;然后,介紹多文件上傳如何實(shí)現(xiàn)。
一、單文件上傳
單文件上傳的原理是將文件數(shù)據(jù)放入request中,由頁(yè)面直接傳遞至后臺(tái)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時(shí),做一些操作
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;//獲取存儲(chǔ)的目標(biāo)地址
file.SaveAs(path);}
return View();
}
這里需要注意的是,在ASP.NET中,request的默認(rèn)大小為4M,因此,如需上傳較大文件,需要更改Web.config。
<system.web> <httpRuntime maxRequestLength="40960"/> </system.web>
二、批量文件上傳
思路是通過(guò)js根據(jù)用戶需求動(dòng)態(tài)添加上傳控件,多個(gè)文件通過(guò)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頁(yè)面代碼中必須保證多個(gè)上傳控件的name屬性值互不相同。
以上便實(shí)現(xiàn)了批量文件上傳。
本人才疏學(xué)淺,僅供大家參考,若有不當(dāng)之處,請(qǐng)大家批評(píng)指正!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC HttpPostedFileBase文件上傳的實(shí)例代碼
- asp.net core mvc實(shí)現(xiàn)文件上傳實(shí)例
- asp.net mvc 實(shí)現(xiàn)文件上傳帶進(jìn)度條的思路與方法
- 解決ASP.NET Core Mvc文件上傳限制問(wèn)題實(shí)例
- asp.net中MVC借助Iframe實(shí)現(xiàn)無(wú)刷新上傳文件實(shí)例
- Asp.net實(shí)現(xiàn)MVC處理文件的上傳下載功能實(shí)例教程
- ASP.NET MVC處理文件上傳的小例子
- 用Html5與Asp.net MVC上傳多個(gè)文件的實(shí)現(xiàn)代碼
- ASP.NET?MVC使用JSAjaxFileUploader插件實(shí)現(xiàn)單文件上傳
相關(guān)文章
使用Supervisor守護(hù)ASP.NET?Core應(yīng)用程序進(jìn)程
這篇文章介紹了使用Supervisor守護(hù)ASP.NET?Core應(yīng)用程序進(jìn)程的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Asp.net Core 1.1 升級(jí)后操作mysql出錯(cuò)的解決辦法
這篇文章主要介紹了Asp.net Core 1.1 升級(jí)后操作mysql出錯(cuò)的解決辦法,需要的朋友可以參考下2016-12-12
在Global.asax文件里實(shí)現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請(qǐng)求)
可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件來(lái)實(shí)現(xiàn)表單或者URL提交數(shù)據(jù)的獲取,獲取后傳給SQLInjectionHelper類ValidUrlData方法來(lái)完成檢查2013-01-01
利用.net core實(shí)現(xiàn)反向代理中間件的方法
這篇文章主要給大家介紹了關(guān)于利用.net core實(shí)現(xiàn)反向代理中間件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
asp.net得到本機(jī)數(shù)據(jù)庫(kù)實(shí)例的兩種方法代碼
這篇文章介紹了asp.net得到本機(jī)數(shù)據(jù)庫(kù)實(shí)例的兩種方法代碼,有需要的朋友可以參考一下2013-07-07
ASP.NET 頁(yè)面?zhèn)髦党S梅椒偨Y(jié)
ASP.NET 頁(yè)面?zhèn)髦档姆绞接泻芏啵疚恼砹艘恍┍容^常用方法,大家可以根據(jù)自己需求自由選擇2013-08-08

