溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用ASP.NET MVC怎么批量上傳文件

發(fā)布時(shí)間:2021-05-14 17:19:35 來(lái)源:億速云 閱讀:237 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用ASP.NET MVC怎么批量上傳文件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、單文件上傳

單文件上傳的原理是將文件數(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>");   
}

ASP.NET 是什么

ASP.NET 是開(kāi)源,跨平臺(tái),高性能,輕量級(jí)的 Web 應(yīng)用構(gòu)建框架,常用于通過(guò) HTML、CSS、JavaScript 以及服務(wù)器腳本來(lái)構(gòu)建網(wǎng)頁(yè)和網(wǎng)站。

上述就是小編為大家分享的使用ASP.NET MVC怎么批量上傳文件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI