当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net批量多选文件上传解决方案

asp.net批量多选文件上传解决方案

2017年12月12日  | 移动技术网IT编程  | 我要评论

美领馆 成都,白领健康网,亮剑精神视频

多选文件上传,已经非常多了,选择性多了可能有时候要比较下哪个更合适,结合到项目中使用更方便才是最重要的。很多的多选上传基本上都是调用的swf文件,确实用flash 或flex开发一个多选上传的功能很方便,比如flex里内置的filereferencelist对象本身就支持文件的多选,有这个的话就方便多了,下面要说的主要也是基于flex开发的一个多选上传功能。
主要实现的功能如下:
  一、选择多个文件上传并显示单个文件的上传进度
  二、显示所有文件总的上传进度
  三、显示所有上传文件的总大小
  四、上传前可以删除任意选定一个或多个文件(按住ctrl或shift键)
  五、asp.net页面调用生成的swf文件异步上传到服务器

先看下演示的截图,如下:

   

大致功能和上面截图一样,下面主要说下asp.net里怎么调用,flex的里面代码我这里就不详细说明了,flex里面的代码不多,文章后面提供下载,用flex3.0或4.0可以打开运行。
  其中有一个地方说明一下,就是在多选删除的地方,为了保证随意多选删除的正确性,需要把选定的索引项降序排序,每次从数组最大处删除,避免循环删除时索引超界。

function deleteitem():void{
 var selectitems:array = process_list.selecteditems;
 var selectindex:array = process_list.selectedindices;
 selectindex = selectindex.sort(2);//索引按降序排序
 var icount:int = selectitems.length;
 var sizemum:number = 0;
 for(var i:int=0;i<icount;i++){
  info.splice(selectindex[i],1);
  fileref.filelist.splice(selectindex[i],1);//移除的选择项按索引从大到小移除,以便移除过程中索引不超界
 }
 for(var j:number=0;j<fileref.filelist.length;j++){      
  sizemum+=fileref.filelist[j].size;     
 }   
 process_list.dataprovider = info;
 tip_txt.text="共"+fileref.filelist.length+"个文件 "+(sizemum/(1024*1024)).tofixed(4).tostring()+"mb";
      
 if(info.length<=0){
  delete_btn.enabled = false;
 }     
}

  调用其实也比较简单,新建一个asp教程x页面加载生成的swf文件,这里生成的文件名是upload.swf,利用flex内置的swfobject.js里面的方法加载,如下:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
 <title>无标题页</title>  
   <style type="text/css教程" media="screen"> 
   html, body { height:100%; }
   body { margin:0; padding:0; overflow:auto; text-align:center; 
     background-color: #ffffff; } 
   #flashcontent { display:none; }
  </style>
   
  <script type="text/网页特效" src="swfobject.js"></script> 
  <script type="text/javascript" >

   var swfversionstr = "10.0.0";
   var xiswfurlstr = "playerproductinstall.swf";
   var flashvars = {};
   flashvars.url = "savefile.aspx?param=id|100,name|测试用户";
   var params = {};
   params.quality = "high";
   params.bgcolor = "#ffffff";
   params.allowscriptaccess = "samedomain";
   params.allowfullscreen = "true";
   var attributes = {};
   attributes.id = "upload";
   attributes.name = "upload";
   attributes.align = "middle";
   swfobject.embedswf(
    "upload.swf", "flashcontent", 
    "587", "370", 
    swfversionstr, xiswfurlstr, 
    flashvars, params, attributes);
 
   function uploadcompelete(){
    //完成后的操作,如页面跳转或关闭当前页
    document.getelementbyid('btnupload').disabled = false;
   }
   function submitform(){
    thismovie("upload").uploadfile();
   }
   function thismovie(moviename) {
    if (navigator.appname.indexof("microsoft") != -1) {
     return window[moviename];
    } else {
     return document[moviename];
    }
   }
   function disabledbutton()
    {
    document.getelementbyid('btnupload').disabled = true;
    }
 
  </script>
</head>
<body>
 <div id="flashcontent" style="width:587px; height:380px">
   </div>
  <br />
  <input id="btnupload" style="width: 71px" type="button" value="上 传" onclick="submitform()" />
</body>
</html>
  如上,页面放置一个按钮,执行upload.swf里面的uploadfile方法,在flex里面其实是回调了uploadhandler方法:
//===================
// 点击上传按钮
//===================
internal function uploadhandler():void{
 if(uploadfile_num!=0) return;
 if(process_list.dataprovider==null || info.length<=0){
  alert.show("您还未选择文件!","提示信息");
  return;
 }
 else
 {
  externalinterface.call("disabledbutton"); //点上传后禁用按钮 
 }
 for(var i:number=0;i<fileref.filelist.length;i++){    
  upload_size_total+=fileref.filelist[i].size;          
 }         
 uploadfile(uploadfile_num); 
 add_btn.enabled = false; //点上传后禁用浏览按钮 
 delete_btn.enabled = false;//点上传后禁用删除按钮 
}
 
  savefile.aspx页面主要是接收并存储文件,如下:
protected void page_load(object sender, eventargs e)
{
 //string param = request["param"];
 string path = server.mappath("files/");
 if (!directory.exists(path))
 {
  directory.createdirectory(path);
 }
 //httpfilecollection files = request.files;
 //string filename = string.empty;
 //for (int i = 0; i < files.count; i++)
 //{
 // filename = path.getfilename(files[i].filename).tolower();
 // files[i].saveas(path + filename);
 //}
 httppostedfile file = request.files["filedata"]; //文件是一个一个异步提交过来,所以不需要循环文件集合
 if (file != null && file.contentlength > 0)
 {
  file.saveas(path+request.form["filename"]);
 }
}

  毕竟不是以文件流的形式接收和存储,所以如果是上传大文件的话,可以看到显示页面已经上传完成100%,但是到这个处理存储页面会停顿一会,接收并存储完成后前台页面才会反应过来。
  还有一点要提一下,就是如果传递的参数包含中文的话,需要config编码格式为utf-8格式,但有原先的系统可能是gb2312格式的,改成utf-8可能对系统有影响,可以单独建一个webconfig,上传的时候就读单独的config。

以上就是asp.net批量多选文件上传代码,希望可以解决大家进行asp.net上传多文件时遇到的问题。

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

相关文章:

验证码:
移动技术网