自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析ASP.NET多附件上傳的實(shí)現(xiàn)

開發(fā) 后端
本文介紹ASP.NET多附件上傳的實(shí)現(xiàn),以及介紹多附件上傳的原理與之類似,只不過需要事先通過腳本在頁面上動(dòng)態(tài)創(chuàng)建多個(gè)input type='file'的標(biāo)簽

在寫這篇文章之前我也在Google上找到了很多有關(guān)多附件上傳的文章,有用ASP.NET多附件上傳實(shí)現(xiàn)的,也有用JSP、PHP等其它技術(shù)實(shí)現(xiàn)的,但基本前提都是事先通過js腳本來動(dòng)態(tài)創(chuàng)建DOM,然后上傳的時(shí)候在服務(wù)端做一下處理,有點(diǎn)類似于163的郵件系統(tǒng)。文件上傳需要通過頁面的POST方法進(jìn)行提交,這個(gè)我在一次MOSS開發(fā)中iFrame表單提交的古怪問題解決一問中已經(jīng)闡述過,其中包括了如何使用頁面隱藏的iFrame來提交表單從而避免整個(gè)頁面提交到服務(wù)器而導(dǎo)致頁面的刷新。多附件上傳的原理與之類似,只不過需要事先通過腳本在頁面上動(dòng)態(tài)創(chuàng)建多個(gè)input type='file'的標(biāo)簽,當(dāng)然,如果要想功能更加完美,你可能還需要通過腳本動(dòng)態(tài)添加一些按鈕事件以讓用戶可以刪除他所添加的文件。下面是ASP.NET多附件上傳的實(shí)現(xiàn)


其中紅色方框內(nèi)的內(nèi)容是通過腳本在頁面上動(dòng)態(tài)創(chuàng)建的,將用戶在客戶端所選文件的文件名動(dòng)態(tài)添加到一個(gè)div里,同時(shí)在這個(gè)div中放一個(gè)隱藏的input type=’file’的標(biāo)簽,它的value為用戶所選文件的路徑,然后在div中放置一個(gè)img,添加onmouseover和onmouseout 事件為圖片增加了一些鼠標(biāo)滑動(dòng)時(shí)的效果,onclick事件用來響應(yīng)用戶點(diǎn)擊img時(shí)刪除對應(yīng)的文件??匆幌翧SP.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插件的實(shí)現(xiàn)方式
  2. 概述ASP.NET應(yīng)用程序
  3. 淺談ASP.NET 2.0數(shù)據(jù)綁定
  4. ASP.NET阻止Java Script注入式攻擊
  5. ASP.NET MVC使用T4
責(zé)任編輯:佚名 來源: 51CTO博客
相關(guān)推薦

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-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET優(yōu)點(diǎn)

2009-07-27 15:34:11

MembershipASP.NET

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-11-06 09:23:41

ASP.NET高效分頁

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2009-07-22 18:03:00

ASP.NET ASP

2009-08-05 16:59:55

ASP.NET組件設(shè)計(jì)

2009-07-24 10:53:51

ASP.NET實(shí)現(xiàn)靜態(tài)

2009-07-28 10:59:13

ASP.NET IIS

2009-07-20 16:23:01

ASP.NET授權(quán)模塊

2009-08-04 17:00:09

ASP.NET禁用Vi

2009-07-23 14:31:20

ASP.NET MVC

2009-07-28 16:40:11

ASP.NET異步頁面
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號