欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET MVC實(shí)現(xiàn)批量文件上傳

 更新時(shí)間:2018年09月08日 15:17:04   作者:愛(ài)吃番茄的胖超人  
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC實(shí)現(xiàn)批量文件上傳,簡(jiǎn)單介紹單文件上傳的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

根據(jù)項(xiàng)目需要,研究了一下如何在ASP.NETMVC下實(shí)現(xiàn)批量文件上傳。首先,介紹單文件上傳;然后,介紹多文件上傳如何實(shí)現(xiàn)。

一、單文件上傳

單文件上傳的原理是將文件數(shù)據(jù)放入request中,由頁(yè)面直接傳遞至后臺(tái)controller中,類(lèi)似于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文件夾必須有讀寫(xiě)權(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論