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

ASP.NET多附件上傳和附件編輯的實(shí)現(xiàn)

開(kāi)發(fā) 后端
本文將簡(jiǎn)單談?wù)凙SP.NET多附件上傳和附件編輯的實(shí)現(xiàn)。多附件上傳也有用JSP、PHP等其它技術(shù)實(shí)現(xiàn)的,但基本前提都是事先通過(guò)js腳本來(lái)動(dòng)態(tài)創(chuàng)建DOM,然后上傳的時(shí)候在服務(wù)端做一下處理。

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

應(yīng)用效果的截圖

其中紅色方框內(nèi)的內(nèi)容是通過(guò)腳本在頁(yè)面上動(dòng)態(tài)創(chuàng)建的,將用戶(hù)在客戶(hù)端所選文件的文件名動(dòng)態(tài)添加到一個(gè)div里,同時(shí)在這個(gè)div中放一個(gè)隱藏的input type=’file’的標(biāo)簽,它的value為用戶(hù)所選文件的路徑,然后在div中放置一個(gè)img,添加onmouseover和onmouseout事件為圖片增加了一些鼠標(biāo)滑動(dòng)時(shí)的效果,onclick事件用來(lái)響應(yīng)用戶(hù)點(diǎn)擊img時(shí)刪除對(duì)應(yīng)的文件??匆幌麓a中的具體實(shí)現(xiàn)。

代碼建立在Ajax.net基礎(chǔ)之上,環(huán)境是Visual Studio 2008 + Windows 2003,測(cè)試通過(guò)!

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml"> 
  5. <head runat="server"> 
  6. <title></title> 
  7. <script src="MultiAffix.js" type="text/javascript"></script> 
  8. <script type="text/javascript"> 
  9. var controlName = 1; // This variable is for the dynamic file controls's name.  
  10.  
  11. function addImg(targetElement, savestatsElement, oldimgElement) {  
  12. var browseimgElement = $get("browseimg");  
  13. var arr = browseimgElement.getElementsByTagName('input');  
  14. if (arr.length == 0 || arr[0].value.length == 0) {  
  15.  
  16. alert('No file inputs.');  
  17. return;  
  18. }  
  19. var oldbrowser = arr[0];  
  20. var filename = getfilename(oldbrowser.value);  
  21. if (!validateimgtype(oldbrowser.value)) return;  
  22. if (!validateimgcount(targetElement, 3)) return;  
  23. var imgtitles = savestatsElement.value + oldimgElement.value;  
  24. if (validateimgexist(filename, imgtitles)) { alert('You have already added this image!'); return; }  
  25. if (oldbrowser != undefined) {  
  26. var newbrowser = oldbrowser.cloneNode(true);  
  27. newbrowser.value = '';  
  28. var newfile = document.createElement('div');  
  29. newfile.innerHTML = filename + '&nbsp;&nbsp;';  
  30.  
  31. // Create a button element for delete the image.  
  32. var newfileimgbutton = 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. <form id="form1" runat="server"> 
  64. <asp:ScriptManager ID="ScriptManager1" runat="server"> 
  65. </asp:ScriptManager> 
  66. <div> 
  67. <div> 
  68. Description:  
  69. <asp:TextBox ID="tbDescription" MaxLength="2000" runat="server" TextMode="MultiLine"></asp:TextBox> 
  70. </div> 
  71. <div> 
  72. Location:  
  73. <asp:DropDownList ID="ddlLocation" runat="server"> 
  74. </asp:DropDownList> 
  75. </div> 
  76. <div> 
  77. Display Posted By User:  
  78. <asp:CheckBox ID="chkPostedByUser" Checked="true" runat="server" /> 
  79. </div> 
  80. <div> 
  81. Notify Shout out User:  
  82. <asp:CheckBox ID="chkNotifyUser" runat="server" /> 
  83. </div> 
  84. <div> 
  85. Notify Shout out to Email:  
  86. <asp:TextBox ID="tbShoutoutToEmail" MaxLength="25" runat="server"></asp:TextBox> 
  87. </div> 
  88. <div> 
  89. Images:  
  90. <div id="saveshoutoutimgs" runat="server"> 
  91. </div> 
  92. <input id="btAddImage" type="button" onclick="$get('saveshoutoutaddimgs').style.display='block';this.disabled=true;" 
  93. value="Click here to Add Image" /> 
  94. </div> 
  95. <div id="saveshoutoutdetailshowimg"> 
  96. <div id="saveshoutoutaddimgs" style="display: none;"> 
  97. <div> 
  98. Add Image:</div> 
  99. <div id="browseimg"> 
  100. <input type="file" /> 
  101. </div> 
  102. <div> 
  103. Size limit of the images is 100kb. Hieght and Width of the images should not exceed  
  104. 200px.</div> 
  105. <div> 
  106. <input id="chkAgree" type="checkbox" onclick="$get('btAdd').disabled=!this.checked;" />I  
  107. agree.legal signoff text to be defined.  
  108. </div> 
  109. <div> 
  110. <input id="btAdd" disabled="disabled" type="button" value="Add" runat="server" /> 
  111. </div> 
  112. </div> 
  113. </div> 
  114. </div> 
  115. <asp:TextBox ID="tbImgs" runat="server" Text="|" Style="display: none;"></asp:TextBox> 
  116. <asp:TextBox ID="tbOldImgs" runat="server" Text="|" Style="display: none;"></asp:TextBox> 
  117. </form> 
  118. </body> 
  119. </html> 
  120. protected void Page_Load(object sender, EventArgs e)  
  121. {  
  122. string script = string.Format("addImg($get('{0}'), $get('{1}'), $get('{2}'));",  
  123. this.saveshoutoutimgs.ClientID,  
  124. this.tbImgs.ClientID,  
  125. this.tbOldImgs.ClientID);  
  126. this.btAdd.Attributes.Add("onclick", script);  

簡(jiǎn)單做一下說(shuō)明:

1. <div id="saveshoutoutimg" runat="server"/>用來(lái)存放動(dòng)態(tài)添加的文件相關(guān)標(biāo)簽。

2. btAddImage被點(diǎn)擊后自身將被disabled掉,然后顯示saveshoutoutaddimgs整個(gè)div。

3. 在saveshoutoutaddimgs中用戶(hù)可以完成文件的選取和確認(rèn)操作,chkAgree用來(lái)enable btAdd按鈕。

4. 當(dāng)用戶(hù)點(diǎn)擊btAdd時(shí),觸發(fā)onclick事件,該事件在code-behind的Page_Load方法中注冊(cè),因?yàn)槟_本中涉及到使用服務(wù)端控件的ClientID屬性,這樣寫(xiě)比較方便。

5. 客戶(hù)端函數(shù)addImg用來(lái)完成動(dòng)態(tài)DOM的添加操作,它接收三個(gè)參數(shù),***個(gè)參數(shù)targetElement表示存放動(dòng)態(tài)DOM的宿主DIV,第二個(gè)參數(shù)savestatsElement表示用于保存已添加文件信息的隱藏文本框,第三個(gè)參數(shù)oldimgElement表示用于保存在編輯狀態(tài)下用戶(hù)上一次上傳的文件信息隱藏文本框。基本思路是復(fù)制browseimg下的input type="file"標(biāo)簽,然后將動(dòng)態(tài)生成的DOM添加到saveshoutoutimgs下,并同時(shí)附加了一些事件。

6. tbImgs隱藏文本框用來(lái)保存用戶(hù)已選文件的信息,以"|文件名1|文件名2|文件名3|..."的格式存放;tbOldImgs隱藏文本框中的值在編輯狀態(tài)下才會(huì)得到,其中保存了用戶(hù)上一次所上傳文件的信息,存儲(chǔ)格式與tbImgs相同。

7. 在編輯狀態(tài)下,在服務(wù)端向saveshoutoutimgs標(biāo)簽添加與addImg腳本函數(shù)所生成的動(dòng)態(tài)DOM相同的標(biāo)簽,并同時(shí)往tbOldImgs隱藏文本框中寫(xiě)入文件信息。我在這里寫(xiě)了一個(gè)示例,讀者可以自己完善代碼用以驗(yàn)證。在顯示文件時(shí)我在文件的名稱(chēng)上添加了一個(gè)鏈接,這個(gè)鏈接所指向的頁(yè)面用于輸出圖片,如通過(guò)得到的圖片ID在數(shù)據(jù)庫(kù)中檢索圖片的二進(jìn)制數(shù)據(jù)然后Write到頁(yè)面上。ImageEntity為自定義Image對(duì)象的實(shí)體類(lèi),用以存儲(chǔ)圖片文件的相關(guān)信息。

  1. public void SetImages(List<ImageEntity> images)  
  2. {  
  3. if (images.Count > 0)  
  4. {  
  5. this.tbOldImgs.Text = "|";  
  6. foreach (ImageEntity image in images)  
  7. {  
  8. HtmlGenericControl imgDiv = new HtmlGenericControl("Div");  
  9.  
  10. HtmlAnchor imgAnchor = new HtmlAnchor();  
  11. imgAnchor.HRef = string.Format("Thumbnail.aspx?isthumbnail=false&basecommentid={0}&imagetitle={1}",  
  12. image.ID.ToString(), image.Title);  
  13. imgAnchor.Target = "_blank";  
  14. imgAnchor.Title = image.Title;  
  15. imgAnchor.InnerHtml = image.Title + "&nbsp;&nbsp;";  
  16.  
  17. HtmlImage imgButton = new HtmlImage();  
  18. imgButton.Src = "ShoutOut_Close.gif";  
  19. imgButton.Alt = "Delete";  
  20. imgButton.Attributes["onclick"] = string.Format("this.parentNode.parentNode.removeChild(this.parentNode);$get('{0}').value = updatehiddenimgs('{1}',$get('{0}').value);",  
  21. this.tbOldImgs.ClientID, image.Title);  
  22. imgButton.Attributes["onmouseover"] = "this.src='ShoutOut_Close_rollover.gif'";  
  23. imgButton.Attributes["onmouseout"] = "this.src='ShoutOut_Close.gif'";  
  24.  
  25. imgDiv.Controls.Add(imgAnchor);  
  26. imgDiv.Controls.Add(imgButton);  
  27. this.saveshoutoutimgs.Controls.Add(imgDiv);  
  28. this.tbOldImgs.Text += image.Title + "|";  
  29. }  
  30. }  
  31. }  
  32.    
  33.  
  34. public class ImageEntity  
  35. {  
  36. public ImageEntity()  
  37. {  
  38. }  
  39.  
  40. public ImageEntity(int id, string title, Byte[] imageBlob, string type)  
  41. {  
  42. ID = id;  
  43. Title = title;  
  44. ImageBlob = imageBlob;  
  45. Type = type;  
  46. }  
  47.  
  48. public int ID { get; set; }  
  49. public string Title { get; set; }  
  50. public string Type { get; set; }  
  51. public Byte[] ImageBlob { get; set; }  

有一個(gè)問(wèn)題需要注意,當(dāng)保存編輯狀態(tài)下的數(shù)據(jù)時(shí),在服務(wù)端需要重新刪除原有圖片然后再重新添加圖片,對(duì)于用戶(hù)未在客戶(hù)端修改的圖片,則需要在保存數(shù)據(jù)前通過(guò)tbOldImgs隱藏域中的相關(guān)信息重新檢索得到圖片數(shù)據(jù),然后重新保存圖片。例如編輯狀態(tài)下得到A、B、C三張圖片,用戶(hù)刪除了圖片C,添加了圖片D,則保存時(shí)在服務(wù)端這樣操作:先通過(guò)tbOldImgs隱藏域得到剩余的舊圖片信息(即圖片A和圖片B),從數(shù)據(jù)庫(kù)中檢索出這些圖片的數(shù)據(jù),保存前先刪除與該條數(shù)據(jù)相關(guān)的所有已上傳圖片,然后與得到的新圖片(即圖片D)一并存入數(shù)據(jù)庫(kù)中。

還有一點(diǎn)就是,如果想要實(shí)現(xiàn)上傳圖片前判斷圖片文件的大小,必須通過(guò)C#代碼來(lái)實(shí)現(xiàn),這個(gè)不能簡(jiǎn)單地通過(guò)js腳本來(lái)實(shí)現(xiàn),因?yàn)樯婕暗綖g覽器對(duì)客戶(hù)端文件訪問(wèn)權(quán)限的限制。也就是說(shuō),需要將頁(yè)面預(yù)先提交到服務(wù)器,判斷完成后通知客戶(hù)端接下來(lái)的行為,為了避免頁(yè)面提交時(shí)客戶(hù)端原有的狀態(tài)丟失,可以采用隱藏iFrame的方式提交頁(yè)面,這個(gè)在文章的一開(kāi)始我已經(jīng)提到過(guò)了。

總之,在Web中實(shí)現(xiàn)多附件上傳必須借助于javascript來(lái)動(dòng)態(tài)創(chuàng)建DOM,編輯狀態(tài)下可以在服務(wù)端將事先處理好的DOM添加到頁(yè)面上,并同時(shí)附件腳本事件,在服務(wù)端代碼和客戶(hù)端代碼之間注意保存各個(gè)控件的狀態(tài)。

【編輯推薦】

  1. 淺談ASP.NET MVC中的FluentHtml與連續(xù)接口
  2. ASP.NET中性能和擴(kuò)展性的秘密
  3. 淺談ASP.NET中ViewState與ViewData的區(qū)別
  4. ASP.NET 3.5圖表控件親密接觸
  5. ASP.NET中防止用戶(hù)多次登錄的方法
責(zé)任編輯:彭凡 來(lái)源: cnblogs
相關(guān)推薦

2009-07-23 10:37:43

2009-07-29 10:02:49

ASP.NET上傳

2009-07-21 15:38:31

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入門(mén)教程

2009-07-29 16:08:07

ASP和ASP.NET

2009-07-27 14:46:16

XML和ASP.NET

2009-07-22 18:03:00

ASP.NET ASP

2009-07-31 13:52:26

ASP.NET數(shù)據(jù)庫(kù)圖

2010-07-01 08:49:34

ASP.NET MVC

2009-07-20 15:44:32

ASP.NET MVC

2024-05-20 13:06:18

2010-02-05 08:32:32

ASP.NET MVC

2009-07-27 17:32:39

Web ServiceASP.NET

2009-07-20 16:09:39

2009-07-21 13:01:07

ASP.NET上傳文件

2009-07-21 16:23:57

2009-07-22 17:45:11

ASP.NET插件

2009-07-28 17:17:19

ASP.NET概述

2009-08-03 14:22:33

什么是ASP.NET

2009-12-07 09:13:05

取消PHP上傳限制
點(diǎn)贊
收藏

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