当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET MVC实现批量文件上传

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

2018年09月21日  | 移动技术网IT编程  | 我要评论

军火贩迅雷下载,顶楼的大象完整版,石家庄电视台小吴帮忙

根据项目需要,研究了一下如何在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属性值互不相同。

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

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网