ASP.NET MVC实现批量文件上传

所属分类: 网络编程 / ASP.NET 阅读数: 688
收藏 0 赞 0 分享

根据项目需要,研究了一下如何在ASP.NETMVC下实现批量文件上传。首先,介绍单文件上传;然后,介绍多文件上传如何实现。

一、单文件上传

单文件上传的原理是将文件数据放入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>");   
}

注意在Request.Files中,不同文件的index是上传控件的name属性值,因此在aspx页面代码中必须保证多个上传控件的name属性值互不相同。

以上便实现了批量文件上传。

本人才疏学浅,仅供大家参考,若有不当之处,请大家批评指正!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

asp.net网站首页根据IP自动跳转指定页面的示例

本文介绍的程序主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net无法获取iis目录的问题解决方法

本文介绍了Asp.Net无法获取IIS拾取目录的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

.net中 发送邮件内容嵌入图片的具体实例

这篇文章主要介绍了.net中 发送邮件内容嵌入图片的具体实例,需要的朋友可以参考下
收藏 0 赞 0 分享

在.net中用CheckBoxList实现单选

用CheckBoxList实现单选的原因是我觉得CheckBoxList控件页面展示效果要好看一些,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP.NET―001:GridView绑定List、页面返回值具体实现

这篇文章主要介绍了ASP.NET―GridView绑定List、页面返回值具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

Ajax实现异步刷新验证用户名是否已存在的具体方法

由于要做一个注册页面,看到许多网站上都是使用Ajax异步刷新验证用户名是否可用的,所以自己也动手做一个小实例
收藏 0 赞 0 分享

ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现

这篇文章主要介绍了ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

.Net消息队列的使用方法

这篇文章主要介绍了.Net消息队列的使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C# web api返回类型设置为json的两种方法

web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面为大家介绍几种不错的方法
收藏 0 赞 0 分享

asp.net获取网站目录物理路径示例

这篇文章主要介绍了asp.net获取网站目录物理路径的方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多