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

发布时间:2021-05-14 17:19:35 作者:Leah
来源:亿速云 阅读:224

这期内容当中小编将会给大家带来有关使用ASP.NET MVC怎么批量上传文件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

一、单文件上传

单文件上传的原理是将文件数据放入request中,由页面直接传递至后台controller中,类似于view和controller之间传参数,直接贴上代码加注释。
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 文件数为0上传不成功
        return View();
      }
  var file = Request.Files[0];
  if (file.ContentLength == 0){
    //文件大小大(以字节为单位)为0时,做一些操作
    return View();
  }
  else{
    //文件大小不为0
    file = Request.Files[0]; //服务器上的UpLoadFile文件夹必须有读写权限
    string target = Server.MapPath("/")+("/Mock/Learning/");//取得目标文件夹的路径
    string filename = file.FileName;//取得文件名字
    string path = target + filename;//获取存储的目标地址
    file.SaveAs(path);}
    return View();
}

这里需要注意的是,在ASP.NET中,request的默认大小为4M,因此,如需上传较大文件,需要更改Web.config。

<system.web>
  <httpRuntime maxRequestLength="40960"/> 
</system.web>

二、批量文件上传

思路是通过js根据用户需求动态添加上传控件,多个文件通过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文件夹是否存在,不存在就创建
    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 是开源,跨平台,高性能,轻量级的 Web 应用构建框架,常用于通过 HTML、CSS、JavaScript 以及服务器脚本来构建网页和网站。

上述就是小编为大家分享的使用ASP.NET MVC怎么批量上传文件了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. ASP.NET在MVC5中使用MiniProfiler监控MVC性能
  2. ASP.NET Core MVC上传、导入和导出文件实例代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net mvc

上一篇:使用.Net怎么上传图片缩略图

下一篇:使用mvc怎么实现一个图片验证码功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》