189 8069 5689

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

在写这篇文章之前我也在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></font></strong> </li> <li><strong><font><</font></strong><font>scriptsrc</font><strong><font>scriptsrc</font></strong>="MultiAffix.js"<font>type</font>="text/javascript"<strong><font>> script></font></strong> </li> <li><strong><font><</font></strong><font>scripttype</font><strong><font>scripttype</font></strong>=<font>"text/javascript"</font><strong><font>></font></strong> </li> <li><font>varcontrolName</font>=<font>1</font>;//Thisvariableisforthedynamicfilecontrols'sname.  </li> <li> </li> <li>functionaddImg(targetElement,savestatsElement,oldimgElement){  </li> <li><font>varbrowseimgElement</font>=$get("browseimg");  </li> <li><font>vararr</font>=<font>browseimgElement</font>.getElementsByTagName('input');  </li> <li>if(<font>arr.length</font>==0||arr[0]<font>.value.length</font>==0){  </li> <li> </li> <li>alert('Nofileinputs.');  </li> <li>return;  </li> <li>}  </li> <li><font>varoldbrowser</font>=<font>arr</font>[0];  </li> <li><font>varfilename</font>=<font>getfilename</font>(oldbrowser.value);  </li> <li>if(!validateimgtype(oldbrowser.value))return;  </li> <li>if(!validateimgcount(targetElement,3))return;  </li> <li><font>varimgtitles</font>=<font>savestatsElement</font>.value+oldimgElement.value;  </li> <li>if(validateimgexist(filename,imgtitles))<br>{alert('Youhavealreadyaddedthisimage!');return;}  </li> <li>if(oldbrowser!=undefined){  </li> <li><font>varnewbrowser</font>=<font>oldbrowser</font>.cloneNode(true);  </li> <li><font>newbrowser.value</font>=<font>''</font>;  </li> <li><font>varnewfile</font>=<font>document</font>.createElement('div');  </li> <li><font>newfile.innerHTML</font>=<font>filename</font>+'  ';  </li> <li> </li> <li>//Createabuttonelementfordeletetheimage.  </li> <li><font>varnewfileimgbutton</font>=<font>document</font>.createElement('img');  </li> <li><font>newfileimgbutton.src</font>=<font>'ShoutOut_Close.gif'</font>;  </li> <li><font>newfileimgbutton.alt</font>=<font>'Delete'</font>;  </li> <li><font>newfileimgbutton.onclick</font>=<font>function</font>(){  </li> <li>this.parentNode.parentNode.removeChild(this.parentNode);  </li> <li><font>savestatsElement.value</font>=<font>updatehiddenimgs</font>(filename,savestatsElement.value);  </li> <li>}  </li> <li><font>newfileimgbutton.onmouseover</font>=<font>function</font>(){  </li> <li><font>this.src</font>=<font>'ShoutOut_Close_rollover.gif'</font>;  </li> <li>}  </li> <li><font>newfileimgbutton.onmouseout</font>=<font>function</font>(){  </li> <li><font>this.src</font>=<font>'ShoutOut_Close.gif'</font>;  </li> <li>}  </li> <li> </li> <li>browseimgElement.replaceChild(newbrowser,oldbrowser);  </li> <li><font>oldbrowser.name</font>=++controlName;  </li> <li><font>oldbrowser.style.display</font>=<font>'none'</font>;  </li> <li>newfile.appendChild(oldbrowser);  </li> <li> </li> <li>newfile.appendChild(newfileimgbutton);  </li> <li>targetElement.appendChild(newfile);  </li> <li> </li> <li>$get("chkAgree")<font>.checked</font>=<font>false</font>;  </li> <li>$get("btAdd")<font>.disabled</font>=<font>true</font>;  </li> <li>savestatsElement.value+=filename+'|';  </li> <li>}  </li> <li>}  </li> <li><strong><font> script></font></strong> </li> <li> </li> <li><strong><font> head></font></strong> </li> <li><strong><font><body></font></strong> </li> <li><strong><font><</font></strong><font>formid</font><strong><font>formid</font></strong>="form1"<font>runat</font>="server"<strong><font>></font></strong> </li> <li><strong><font><</font></strong><font>asp:ScriptManagerID</font><strong><font>asp:ScriptManagerID</font></strong>="ScriptManager1"<font>runat</font>="server"<strong><font>></font></strong> </li> <li><strong><font> asp:ScriptManager></font></strong> </li> <li><strong><font><div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>Description:  </li> <li><strong><font><</font></strong><font>asp:TextBoxID</font><strong><font>asp:TextBoxID</font></strong>="tbDescription"<font>MaxLength</font>="2000"<font>runat</font>=<br>"server"<font>TextMode</font>="MultiLine"<strong><font>> asp:TextBox></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>Location:  </li> <li><strong><font><</font></strong><font>asp:DropDownListID</font><strong><font>asp:DropDownListID</font></strong>="ddlLocation"<font>runat</font>="server"<strong><font>></font></strong> </li> <li><strong><font> asp:DropDownList></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>DisplayPostedByUser:  </li> <li><strong><font><</font></strong><font>asp:CheckBoxID</font><strong><font>asp:CheckBoxID</font></strong>="chkPostedByUser"<font>Checked</font>="true"<font>runat</font>=<font>"server"</font><strong><font>/></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>NotifyShoutoutUser:  </li> <li><strong><font><</font></strong><font>asp:CheckBoxID</font><strong><font>asp:CheckBoxID</font></strong>="chkNotifyUser"<font>runat</font>="server"<strong><font>/></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>NotifyShoutouttoEmail:  </li> <li><strong><font><</font></strong><font>asp:TextBoxID</font><strong><font>asp:TextBoxID</font></strong>="tbShoutoutToEmail"<br><font>MaxLength</font>="25"<font>runat</font>=<font>"server"</font><strong><font>> asp:TextBox></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>Images:  </li> <li><strong><font><</font></strong><font>divid</font><strong><font>divid</font></strong>="saveshoutoutimgs"<font>runat</font>="server"<strong><font>></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><</font></strong><font>inputid</font><strong><font>inputid</font></strong>="btAddImage"<font>type</font>="button"<font>onclick</font>=<font>"$get('saveshoutoutaddimgs').<br>style.display='block';this.disabled=true;"</font> </li> <li><font>value</font>=<font>"ClickheretoAddImage"</font><strong><font>/></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><</font></strong><font>divid</font><strong><font>divid</font></strong>=<font>"saveshoutoutdetailshowimg"</font><strong><font>></font></strong> </li> <li><strong><font><</font></strong><font>divid</font><strong><font>divid</font></strong>="saveshoutoutaddimgs"<font>style</font>="display:none;"<strong><font>></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>AddImage:<strong><font> div></font></strong> </li> <li><strong><font><</font></strong><font>divid</font><strong><font>divid</font></strong>=<font>"browseimg"</font><strong><font>></font></strong> </li> <li><strong><font><</font></strong><font>inputtype</font><strong><font>inputtype</font></strong>=<font>"file"</font><strong><font>/></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li>Sizelimitoftheimagesis100kb.HieghtandWidthoftheimagesshouldnotexceed  </li> <li>200px.<strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li><strong><font><</font></strong><font>inputid</font><strong><font>inputid</font></strong>="chkAgree"<font>type</font>="checkbox"<font>onclick</font>=<font>"$get('btAdd').<br>disabled=!this.checked;"</font><strong><font>/></font></strong>I  </li> <li>agree.legalsignofftexttobedefined.  </li> <li><strong><font> div></font></strong> </li> <li><strong><font><div></font></strong> </li> <li><strong><font><</font></strong><font>inputid</font><strong><font>inputid</font></strong>="btAdd"<font>disabled</font>="disabled"<font>type</font>="button"<font>value</font>="Add"<font>runat</font>=<font>"server"</font><strong><font>/></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font> div></font></strong> </li> <li><strong><font><</font></strong><font>asp:TextBoxID</font><strong><font>asp:TextBoxID</font></strong>="tbImgs"<font>runat</font>="server"<font>Text</font>="|"<font>Style</font>=<br>"display:none;"<strong><font>> asp:TextBox></font></strong> </li> <li><strong><font><</font></strong><font>asp:TextBoxID</font><strong><font>asp:TextBoxID</font></strong>="tbOldImgs"<font>runat</font>="server"<font>Text</font>="|"<font>Style</font>=<br>"display:none;"<strong><font>> asp:TextBox></font></strong> </li> <li><strong><font> form></font></strong> </li> <li><strong><font> body></font></strong> </li> <li><strong><font> html></font></strong> </li> </ol></pre><p>【编辑推荐】</p> <ol> <li><font>ASP.NET插件的实现方式</font></li> <li><font>概述ASP.NET应用程序</font></li> <li><font>浅谈ASP.NET 2.0数据绑定</font></li> <li><font>ASP.NET阻止Java Script注入式攻击</font></li> <li><font>ASP.NET MVC使用T4</font></li> </ol> <br> 网页题目:浅析ASP.NET多附件上传的实现 <br> 当前链接:<a href="http://www.cdxtjz.cn/article/cdcdjdd.html">http://www.cdxtjz.cn/article/cdcdjdd.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/cddeosp.html">时间戳转换日期公式</a> </li><li> <a href="/article/cddeogo.html">让Linux自学成才的技巧(怎么自学linux)</a> </li><li> <a href="/article/cddeohi.html">利用Linux轻松搭建高效网关系统(linux搭建网关)</a> </li><li> <a href="/article/cddedoh.html">微信删除怎么恢复?(微信删除怎么恢复聊天记录)</a> </li><li> <a href="/article/cddedds.html">第五人格国际服怎么登录安卓?手机如何登陆国外服务器</a> </li> </ul> </div> </div> <footer> <div class="foot container"> <div class="footl fl"> <h3>联系我们</h3> <dl> 您好HELLO!<br> 感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。 </dl> <ul> <li>电话:028- <span>86922220 18980695689</span></li> <li>商务合作邮箱:631063699@qq.com</li> <li>合作QQ: 532337155</li> <li>成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼</li> </ul> </div> <div class="footr fr"> <h3>小谭建站工作室</h3> <dl> 成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务! </dl> <h3>小谭观点</h3> <dl> 相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。<br> 我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。 </dl> </div> </div> <div class="link"> <div class="container"> <span> 友情链接:</span> <a href="http://www.cdxwcx.cn/bj/" title="网站制作报价" target="_blank">网站制作报价</a>   <a href="http://www.hyjierui.cn/" title="成都注册公司" target="_blank">成都注册公司</a>   <a href="http://www.cdlinhua.com/" title="成都茶叶销售" target="_blank">成都茶叶销售</a>   <a href="http://www.4006tel.net/mobile/" title="Android开发" target="_blank">Android开发</a>   <a href="http://www.scfdjzulin.com/" title="四川发电机组出租" target="_blank">四川发电机组出租</a>   <a href="http://www.cdymzj.com/" title="网站空间" target="_blank">网站空间</a>   <a href="http://www.cdxwcx.cn/tuoguan/hk.html" title="香港机房服务器托管" target="_blank">香港机房服务器托管</a>   <a href="https://www.cdcxhl.com/shoulu/" title="网站快速收录" target="_blank">网站快速收录</a>   <a href="http://www.scghjhjc.cn/" title="四川锦华橡塑" target="_blank">四川锦华橡塑</a>   <a href="http://www.tfchn.cn/" title="传美奇电气" target="_blank">传美奇电气</a>    </div> </div> <div class="copy"> © Copyright 2023 <a href="http://www.cdxtjz.cn/">小谭建站工作室</a>All Rights Reserved.  <a href="https://www.cdxwcx.com" target="_blank">成都网站建设</a> / <a href="https://www.cdcxhl.com/" target="_blank">成都网站建设</a> / <a href="https://www.cdxwcx.com" target="_blank">响应式网站建设</a> / <a href="https://www.cdcxhl.com/zuyong/" target=" _blank">成都服务器租用</a></div> </footer> </body> </html> <script> $(".con img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>