浅析ASP.NET多附件上传的实现

开发 后端
本文介绍ASP.NET多附件上传的实现,以及介绍多附件上传的原理与之类似,只不过需要事先通过脚本在页面上动态创建多个input type='file'的标签

在写这篇文章之前我也在Google上找到了很多有关多附件上传的文章,有用ASP.NET多附件上传实现的,也有用JSP、PHP等其它技术实现的,但基本前提都是事先通过js脚本来动态创建DOM,然后上传的时候在服务端做一下处理,有点类似于163的邮件系统。文件上传需要通过页面的POST方法进行提交,这个我在一次MOSS开发中iFrame表单提交的古怪问题解决一问中已经阐述过,其中包括了如何使用页面隐藏的iFrame来提交表单从而避免整个页面提交到服务器而导致页面的刷新。多附件上传的原理与之类似,只不过需要事先通过脚本在页面上动态创建多个input type='file'的标签,当然,如果要想功能更加完美,你可能还需要通过脚本动态添加一些按钮事件以让用户可以删除他所添加的文件。下面是ASP.NET多附件上传的实现


其中红色方框内的内容是通过脚本在页面上动态创建的,将用户在客户端所选文件的文件名动态添加到一个div里,同时在这个div中放一个隐藏的input type=’file’的标签,它的value为用户所选文件的路径,然后在div中放置一个img,添加onmouseover和onmouseout 事件为图片增加了一些鼠标滑动时的效果,onclick事件用来响应用户点击img时删除对应的文件。看一下ASP.NET多附件上传的代码。

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind=
    "Default.aspx.cs"
    Inherits="WebApplication1._Default"%> 
  2.  
  3. //EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 
  5. <headrunatheadrunat="server"> 
  6. <title>title> 
  7. <scriptsrcscriptsrc="MultiAffix.js"type="text/javascript">script> 
  8. <scripttypescripttype="text/javascript"> 
  9. varcontrolName=1;//Thisvariableisforthedynamicfilecontrols'sname.  
  10.  
  11. functionaddImg(targetElement,savestatsElement,oldimgElement){  
  12. varbrowseimgElement=$get("browseimg");  
  13. vararr=browseimgElement.getElementsByTagName('input');  
  14. if(arr.length==0||arr[0].value.length==0){  
  15.  
  16. alert('Nofileinputs.');  
  17. return;  
  18. }  
  19. varoldbrowser=arr[0];  
  20. varfilename=getfilename(oldbrowser.value);  
  21. if(!validateimgtype(oldbrowser.value))return;  
  22. if(!validateimgcount(targetElement,3))return;  
  23. varimgtitles=savestatsElement.value+oldimgElement.value;  
  24. if(validateimgexist(filename,imgtitles))
    {alert('Youhavealreadyaddedthisimage!');return;}  
  25. if(oldbrowser!=undefined){  
  26. varnewbrowser=oldbrowser.cloneNode(true);  
  27. newbrowser.value='';  
  28. varnewfile=document.createElement('div');  
  29. newfile.innerHTML=filename+'  ';  
  30.  
  31. //Createabuttonelementfordeletetheimage.  
  32. varnewfileimgbutton=document.createElement('img');  
  33. newfileimgbutton.src='ShoutOut_Close.gif';  
  34. newfileimgbutton.alt='Delete';  
  35. newfileimgbutton.onclick=function(){  
  36. this.parentNode.parentNode.removeChild(this.parentNode);  
  37. savestatsElement.value=updatehiddenimgs(filename,savestatsElement.value);  
  38. }  
  39. newfileimgbutton.onmouseover=function(){  
  40. this.src='ShoutOut_Close_rollover.gif';  
  41. }  
  42. newfileimgbutton.onmouseout=function(){  
  43. this.src='ShoutOut_Close.gif';  
  44. }  
  45.  
  46. browseimgElement.replaceChild(newbrowser,oldbrowser);  
  47. oldbrowser.name=++controlName;  
  48. oldbrowser.style.display='none';  
  49. newfile.appendChild(oldbrowser);  
  50.  
  51. newfile.appendChild(newfileimgbutton);  
  52. targetElement.appendChild(newfile);  
  53.  
  54. $get("chkAgree").checked=false;  
  55. $get("btAdd").disabled=true;  
  56. savestatsElement.value+=filename+'|';  
  57. }  
  58. }  
  59. script> 
  60.  
  61. head> 
  62. <body> 
  63. <formidformid="form1"runat="server"> 
  64. <asp:ScriptManagerIDasp:ScriptManagerID="ScriptManager1"runat="server"> 
  65. asp:ScriptManager> 
  66. <div> 
  67. <div> 
  68. Description:  
  69. <asp:TextBoxIDasp:TextBoxID="tbDescription"MaxLength="2000"runat=
    "server"
    TextMode="MultiLine">asp:TextBox> 
  70. div> 
  71. <div> 
  72. Location:  
  73. <asp:DropDownListIDasp:DropDownListID="ddlLocation"runat="server"> 
  74. asp:DropDownList> 
  75. div> 
  76. <div> 
  77. DisplayPostedByUser:  
  78. <asp:CheckBoxIDasp:CheckBoxID="chkPostedByUser"Checked="true"runat="server"/> 
  79. div> 
  80. <div> 
  81. NotifyShoutoutUser:  
  82. <asp:CheckBoxIDasp:CheckBoxID="chkNotifyUser"runat="server"/> 
  83. div> 
  84. <div> 
  85. NotifyShoutouttoEmail:  
  86. <asp:TextBoxIDasp:TextBoxID="tbShoutoutToEmail"
    MaxLength="25"runat="server">asp:TextBox> 
  87. div> 
  88. <div> 
  89. Images:  
  90. <dividdivid="saveshoutoutimgs"runat="server"> 
  91. div> 
  92. <inputidinputid="btAddImage"type="button"onclick="$get('saveshoutoutaddimgs').
    style.display='block';this.disabled=true;"
     
  93. value="ClickheretoAddImage"/> 
  94. div> 
  95. <dividdivid="saveshoutoutdetailshowimg"> 
  96. <dividdivid="saveshoutoutaddimgs"style="display:none;"> 
  97. <div> 
  98. AddImage:div> 
  99. <dividdivid="browseimg"> 
  100. <inputtypeinputtype="file"/> 
  101. div> 
  102. <div> 
  103. Sizelimitoftheimagesis100kb.HieghtandWidthoftheimagesshouldnotexceed  
  104. 200px.div> 
  105. <div> 
  106. <inputidinputid="chkAgree"type="checkbox"onclick="$get('btAdd').
    disabled=!this.checked;"
    />I  
  107. agree.legalsignofftexttobedefined.  
  108. div> 
  109. <div> 
  110. <inputidinputid="btAdd"disabled="disabled"type="button"value="Add"runat="server"/> 
  111. div> 
  112. div> 
  113. div> 
  114. div> 
  115. <asp:TextBoxIDasp:TextBoxID="tbImgs"runat="server"Text="|"Style=
    "display:none;"
    >asp:TextBox> 
  116. <asp:TextBoxIDasp:TextBoxID="tbOldImgs"runat="server"Text="|"Style=
    "display:none;"
    >asp:TextBox> 
  117. form> 
  118. body> 
  119. html> 

【编辑推荐】

  1. ASP.NET插件的实现方式
  2. 概述ASP.NET应用程序
  3. 浅谈ASP.NET 2.0数据绑定
  4. ASP.NET阻止Java Script注入式攻击
  5. ASP.NET MVC使用T4
责任编辑:佚名 来源: 51CTO博客
相关推荐

2009-06-17 10:21:34

ASP.NET多附件上

2009-07-20 16:09:39

2009-08-04 10:02:36

中国站长站

2009-07-28 10:01:16

ASP.NET Exc

2009-07-27 10:18:12

TypeResolveASP.NET

2009-07-27 15:34:11

MembershipASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET优点

2009-11-06 09:23:41

ASP.NET高效分页

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-08-05 16:59:55

ASP.NET组件设计

2009-07-24 10:53:51

ASP.NET实现静态

2009-07-22 18:03:00

ASP.NET ASP

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-07-29 10:02:49

ASP.NET上传

2009-07-28 10:59:13

ASP.NET IIS

2009-07-29 14:12:45

ASP.NET tra

2009-07-28 16:40:11

ASP.NET异步页面

2009-08-05 14:46:17

ASP.NET url
点赞
收藏

51CTO技术栈公众号