45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:怎么样通过c# .net上传多个文件?

怎么样通过c# .net上传多个文件?

2016-09-01 17:15:48 来源:www.45fan.com 【

怎么样通过c# .net上传多个文件?

shawl.qiu c# .net upload v1.0

说明:
最近刚开始学习 .net, 俺本来是打算先从 JScript.net 下手...
不过在CSDN论坛发个帖子征求意见后...还是决定从.NET的主流语言 C#入手...
毕竟整个.NET Framework都是C#写的...
然后, 我发现 .NET最 重要的是它的 .NET Framework, 至于用什么语言应该差不多.
唉, 不说了, 目前这种一知半解的水平说多了会挨砖头...

目录:
1. upload.cs
2.x.aspx调用演示

shawl.qiu
2007-01-30
http://blog.csdn.net/btbtd

内容:
1. upload.cs
  1. usingSystem;
  2. usingSystem.Collections;
  3. usingSystem.Data;
  4. usingSystem.IO;
  5. usingSystem.Text.RegularExpressions;
  6. usingSystem.Web;
  7.  
  8. /*-----------------------------------------------------------------------------------*/
  9. *shawl.qiuc#.netuploadv1.0
  10. /*-----------------------------------------------------------------------------------*/
  11. //---------------------------------------------------------------------beginclassupload
  12. publicclassupload
  13. {
  14. //-----------------------------------beginevent
  15. publicupload()
  16. {
  17. }
  18.  
  19. ~upload()
  20. {
  21. }
  22. //-----------------------------------endevent
  23.  
  24. //-----------------------------------beginpublicconstant
  25. //-----------------------beginabout
  26. publicconststringauSubject="shawl.qiuc#.netupload";
  27. publicconststringauVersion="v1.0";
  28. publicconststringau="shawl.qiu";
  29. publicconststringauEmail="shawl.qiu@gmail.com";
  30. publicconststringauBlog="http://blog.csdn.net/btbtd";
  31. publicconststringauCreateDate="2007-1-30";
  32. //-----------------------endabout
  33. //-----------------------------------endpublicconstant
  34.  
  35. //-----------------------------------beginprivateconstant
  36. //-----------------------------------endprivateconstant
  37.  
  38. //-----------------------------------beginpublicstaticmethod
  39. //-----------------------------------endpublicstaticmethod
  40.  
  41. //-----------------------------------beginprivatestaticmethod
  42. //-----------------------------------endprivatestaticmethod
  43.  
  44. //-----------------------------------beginpublicvariable
  45. publicBooleandebug=false;
  46. publicBooleanautorename=false;
  47.  
  48. publicStringpath="/uploads/";
  49. publicStringext="7z,rar,zip,mp3,bmp,gif,jpg,jpeg,png,txt,swf";
  50. publicStringgoback_url="?";
  51. publicStringallFilePath="";
  52.  
  53. publicInt32interval=10;
  54. publicInt32goback_second=10;
  55. publicInt32item=5;
  56. publicInt32totalsize=1024*5;
  57. publicInt32singlesize=1024;
  58. //-----------------------------------endpublicvariable
  59.  
  60. //-----------------------------------beginpublicmethod
  61. publicvoidinfo(System.Web.UI.HtmlControls.HtmlContainerControloSpan)
  62. {
  63. oSpan.InnerHtml="<ol>"+
  64. "<li>允许上传的文件类型<b>:"+this.ext+"</b></li>"+
  65. "<li>每次总上传大小最大为<b>:"+this.totalsize+"kb</b>,每文件最大为<b>"+
  66. this.singlesize+"kb</b>.</li>"+
  67. "<li>每次上传时间间隔为<b>:"+this.interval+"</b>秒.</li>"+
  68. "</ol>";
  69. }
  70.  
  71. publicvoidgetItem(System.Web.UI.WebControls.Repeaterrpt)
  72. {
  73. DataTabledt=newDataTable();
  74. DataRowdr;
  75. dt.Columns.Add(newDataColumn("id",typeof(Int32)));
  76.  
  77. for(inti=0;i<this.item;i++)
  78. {
  79. dr=dt.NewRow();
  80. dr[0]=i;
  81. dt.Rows.Add(dr);
  82. }//endfor
  83.  
  84. rpt.DataSource=newDataView(dt);
  85. rpt.DataBind();
  86. }//endpublicvoidgetItem
  87.  
  88. publicvoidgo()
  89. {
  90. if(HttpContext.Current.Session["sqUpFl"]==null)
  91. {
  92. HttpContext.Current.Session["sqUpFl"]=DateTime.Now;
  93. }
  94.  
  95. DateTimedtSession=DateTime.Parse(HttpContext.Current.Session["sqUpFl"]+"");
  96.  
  97. if(dtSession>DateTime.Now)
  98. {
  99. TimeSpanhasInterval=dtSession-DateTime.Now;
  100.  
  101. finished("每次上传时间间隔为"+this.interval+"秒,请稍候...",
  102. hasInterval.Seconds,this.goback_url);
  103. gotoEnd;
  104. }
  105.  
  106. //---------------------检测上传目录是否存在
  107. Stringpath_phs=HttpContext.Current.Server.MapPath(path);
  108. if(!Directory.Exists(path_phs))
  109. {
  110. HttpContext.Current.
  111. Response.Write("<h2>指定的上传目录不存在,操作被取消.</h2>");
  112. gotoEnd;
  113. }
  114.  
  115. Int32upTotal=HttpContext.Current.Request.ContentLength/1024;
  116. Int32total=HttpContext.Current.Request.Files.AllKeys.Length;
  117.  
  118. ArrayListaUpsize=newArrayList();
  119. ArrayListaNoExt=newArrayList();
  120. ArrayListaNotAllowExt=newArrayList();
  121. ArrayListaFinished=newArrayList();
  122.  
  123. if(upTotal>this.totalsize)gotoUpsize;
  124.  
  125. for(Int32i=0;i<total;i++)
  126. {
  127. System.Web.HttpPostedFilefiles=HttpContext.Current.Request.Files[i];
  128. if(files.ContentLength>0)
  129. {
  130. Stringflnm=Path.GetFileName(files.FileName);
  131. Stringjustnm=Path.GetFileNameWithoutExtension(files.FileName);
  132. Stringjustext=Path.GetExtension(files.FileName);
  133.  
  134. Stringflph=this.path+justnm+justext;
  135. Stringflph_phs=HttpContext.Current.Server.MapPath(flph);
  136.  
  137. //-----------------------------------单文件超出限制大小
  138. Int32flSize=files.ContentLength/1024;
  139. if(flSize>this.singlesize)
  140. {
  141. aUpsize.Add("文件:"+flnm+",大小为:"+flSize+"kb.");
  142. continue;
  143. }
  144.  
  145. if(!Path.HasExtension(flnm))
  146. {
  147. aNoExt.Add("文件:"+flnm+",没有扩展名.");
  148. continue;
  149. }
  150.  
  151. StringextTemp=Regex.Replace(justext,"^.","");
  152. if(!Regex.IsMatch(this.ext,"//b"+extTemp+"//b",RegexOptions.IgnoreCase))
  153. {
  154. aNotAllowExt.Add("不允许上传的文件扩展:"+flnm);
  155. continue;
  156. }
  157.  
  158. if(File.Exists(flph_phs))//------------------已存在相同同名文件
  159. {
  160. if(autorename)
  161. {
  162. Int32iRename=1;
  163. while(true)
  164. {
  165. StringsNameTemp=HttpContext.Current.
  166. Server.MapPath(this.path+justnm+"_"+(iRename++)+justext);
  167. if(!File.Exists(sNameTemp))
  168. {
  169. flph_phs=sNameTemp;
  170. gotoSaveFile;
  171. }//endif3
  172. }//endwhile
  173. }//endif2
  174. }//endif1
  175. SaveFile:;
  176. files.SaveAs(flph_phs);
  177.  
  178. StringflnmTemp=Path.GetFileName(flph_phs);
  179. aFinished.Add("文件:"+flnmTemp+"已上传.");
  180.  
  181. this.allFilePath+=this.path+flnmTemp+",";
  182.  
  183. HttpContext.Current.Session["sqUpFl"]=
  184. DateTime.Now.AddSeconds(this.interval);
  185. }//endif
  186. }//endfor
  187. gotoReport;
  188.  
  189. Upsize:
  190. HttpContext.Current.Response.Write("<h2>上传大小超出限制,已被终止.</h2>");
  191. gotoEnd;
  192.  
  193. Report:
  194. listAl("已上传文件",aFinished);
  195. listAl("单文件超出限制大小的有",aUpsize);
  196. listAl("没有文件扩展名的有",aNoExt);
  197. listAl("不允许上传的文件扩展有",aNotAllowExt);
  198. gotoFinished;
  199.  
  200. Finished:
  201. finished("操作已完毕",this.goback_second,this.goback_url);
  202. End:
  203. this.allFilePath=Regex.Replace(this.allFilePath,",$","");
  204. }//endgo
  205. //-----------------------------------endpublicmethod
  206.  
  207. //-----------------------------------beginprivatevariable
  208. //-----------------------------------endprivatevariable
  209.  
  210. //-----------------------------------beginprivatemethod
  211. privatevoidlistAl(StringsDetail,ArrayListoAl)
  212. {
  213. if(oAl.Count>0)
  214. {
  215. HttpContext.Current.Response.Write("<ol><label>"+sDetail+"<b>"+
  216. oAl.Count+"个</b>.</label>");
  217. foreach(ObjectiteminoAl)
  218. {
  219. HttpContext.Current.Response.Write("<li>"+item+"</li>");
  220. }
  221. HttpContext.Current.Response.Write("</ol><hr/>");
  222. }
  223. }
  224.  
  225. privatevoidfinished(StringsPrompt,Int32iSecond,StringsUrl)
  226. {
  227. HttpContext.Current.Response.Write("<scripttype=/"text/javascript/">/n");
  228. HttpContext.Current.Response.Write("//<![CDATA[/n");
  229. HttpContext.Current.Response.Write("vartemp=onload;/n");
  230. HttpContext.Current.Response.Write("onload=function(){/n");
  231. HttpContext.Current.Response.Write("try{temp()}catch(e){}/n");
  232. HttpContext.Current.Response.Write("fTimer("+iSecond+",'timer',10);/n");
  233. HttpContext.Current.Response.Write("}/n");
  234. HttpContext.Current.Response.Write("functionfTimer(iTimestamp,sId,iMs){/n");
  235. HttpContext.Current.Response.Write("if(!(iTimestamp.constructor==Date)){/n");
  236. HttpContext.Current.Response.Write("varsqTimeStamp=newDate();/n");
  237. HttpContext.Current.Response.Write("sqTimeStamp.setSeconds(sqTimeStamp.getSeconds()+iTimestamp);/n");
  238. HttpContext.Current.Response.Write("iTimestamp=sqTimeStamp;/n");
  239. HttpContext.Current.Response.Write("}/n");
  240. HttpContext.Current.Response.Write("vartl=arguments.callee;/n");
  241. HttpContext.Current.Response.Write("if(typeofsId=='string'){/n");
  242. HttpContext.Current.Response.Write("varoEle=document.getElementById(sId);/n");
  243. HttpContext.Current.Response.Write("}else{/n");
  244. HttpContext.Current.Response.Write("varoEle=sId;/n");
  245. HttpContext.Current.Response.Write("}/n");
  246. HttpContext.Current.Response.Write("vardt=newDate();/n");
  247. HttpContext.Current.Response.Write("variCk=((iTimestamp.getTime()-dt.getTime())/1000).toFixed(3);/n");
  248. HttpContext.Current.Response.Write("if(iCk<=0){/n");
  249. HttpContext.Current.Response.Write("oEle.innerHTML='00.000';/n");
  250. HttpContext.Current.Response.Write("returnfalse;/n");
  251. HttpContext.Current.Response.Write("}else{/n");
  252. HttpContext.Current.Response.Write("oEle.innerHTML=iCk;/n");
  253. HttpContext.Current.Response.Write("variTimer=setTimeout(function(){tl(iTimestamp,oEle,iMs)},iMs);/n");
  254. HttpContext.Current.Response.Write("}/n");
  255. HttpContext.Current.Response.Write("}//endfunctionfTimer//shawl.qiuscript/n");
  256. HttpContext.Current.Response.Write("//]]>/n");
  257. HttpContext.Current.Response.Write("</script>/n");
  258.  
  259. HttpContext.Current.
  260. Response.Write("<metahttp-equiv=/"Content-Type/"content=/"text/html;charset=utf-8/"/>");
  261. HttpContext.Current.
  262. Response.Write("<metahttp-equiv=/"refresh/"content=/""+iSecond+";URL="+sUrl+"/">");
  263. HttpContext.Current.
  264. Response.Write("<divstyle=/"display:table;width:100%;background-color:yellow!important;"+
  265. "color:black!important;text-align:center!important;/">"+
  266. sPrompt+",<spanid='timer'>"+iSecond+"</span>秒后返回.</div>");
  267. }
  268. //-----------------------------------endprivatemethod
  269.  
  270. //-----------------------------------beginpublicproperty
  271. //-----------------------------------endpublicproperty
  272.  
  273. //-----------------------------------beginprivateproperty
  274. //-----------------------------------endprivateproperty
  275. }
  276. //---------------------------------------------------------------------endclassupload
  277.  
  278.  
2.x.aspx调用演示
  1. <%@PageLanguage="C#"src="cs/upload.cs"AutoEventWireup="True"%>
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
  6. <title>shawl.qiutemplate</title>
  7. </head>
  8. <body>
  9. <scriptrunat="server">
  10. voidPage_Load(Objects,EventArgse)
  11. {
  12. uploadup=newupload();
  13. up.item=5;
  14. up.info(spnInfo);
  15. up.getItem(aspRpt);
  16. up=null;
  17. }
  18.  
  19. voidbtn_up(Objects,EventArgse)
  20. {
  21. uploadup=newupload();
  22.  
  23. up.debug=true;
  24. up.path="/uploads/";//上传目录;
  25. up.goback_url=Request.Url+"";//上传完毕后返回的URL
  26. up.goback_second=20;//上传后返回间隔
  27. up.interval=20;//每次上传间隔,单位秒
  28. up.autorename=true;//自动重命名;
  29. up.go();//执行上传操作;
  30. //Response.Write(up.allFilePath);//返回已上传的所有文件路径,以逗号(,)分隔.
  31.  
  32. up=null;
  33. }
  34.  
  35. </script>
  36. <formrunat="server">
  37. <spanid='spnReport'runat='server'></span>
  38. <spanid='spnInfo'runat='server'></span>
  39. <ol>
  40. <asp:Repeaterid="aspRpt"runat="server">
  41. <ItemTemplate>
  42. <li><inputtype='file'runat='server'/></li>
  43. </ItemTemplate>
  44. </asp:Repeater>
  45. </ol>
  46. <li/><inputtype='submit'OnServerClick="btn_up"runat='server'/>
  47. </form>
  48. </body>
  49. </html>
  50.  
 

本文地址:http://www.45fan.com/a/question/70932.html
Tags: 上传 .net 多个
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部