成都网站建设设计

将想法与焦点和您一起共享

浅析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://chengdu.cdxwcx.cn/article/cdcdjdd.html">http://chengdu.cdxwcx.cn/article/cdcdjdd.html</a> </div> </div> </div> <!--左边end--> <!--右边begin--> <div class="news_r"> <div class="news_t"><h2 class="h2">其他资讯</h2></div> <div class="news_ul3"> <ul> <li> <a href="/article/dhcjohc.html"> <h3 class="h3">桌面虚拟化与虚拟化工具的使用</h3> </a> </li><li> <a href="/article/dhcjops.html"> <h3 class="h3">什么是www二级域名,www二级域名的作用和意义</h3> </a> </li><li> <a href="/article/dhcjdod.html"> <h3 class="h3">python字符串小写</h3> </a> </li><li> <a href="/article/dhcjoic.html"> <h3 class="h3">在服务器上传了网站,结果本机不能访问,服务器该怎么设置?(上传文件无法上传-虚拟主机/数据库问题)</h3> </a> </li><li> <a href="/article/dhcjojh.html"> <h3 class="h3">高防cdn介绍,高防cdn是什么(高防cdn什么意思)</h3> </a> </li> </ul> </div> </div> <!--右边end--> <div class="c_l"></div> </div> </div> <!--正文end--> <!--尾部begin--> <!--尾部begin--> <footer> <div class="f_bg"> <div class="wrap"> <div class="links"> <h2 class="h2">甜橘子解决方案<a href="/solution/" title="更多" class="more">更多+</a></h2> <ul> <li><a href="/solution/xiaochengxu.html" title="小程序定制解决方案">小程序定制解决方案</a></li> <li><a href="/solution/qiyewz.html" title="企业网站建设解决方案">企业网站建设解决方案</a></li> <li><a href="/solution/menhuwz.html" title="行业门户网站建设解决方案">行业门户网站建设解决方案</a></li> <li><a href="/solution/yingxiaowz.html" title="营销型网站建设解决方案">营销型网站建设解决方案</a></li> <li><a href="/solution/waimaowz.html" title="外贸网站建设解决方案">外贸网站建设解决方案</a></li> <li><a href="/solution/pingpaiwz.html" title="品牌形象网站建设解决方案">品牌形象网站建设解决方案</a></li> <li><a href="/solution/dianziwz.html" title="数码、电子产品网站建设解决方案">数码、电子产品网站建设解决方案</a></li> <li><a href="/solution/jituanwz.html" title="集团、上市企业网站建设解决方案">集团、上市企业网站建设解决方案</a></li> <li><a href="/solution/dichanwz.html" title="房地产、地产项目网站建设解决方案">房地产、地产项目网站建设解决方案</a></li> <li><a href="/solution/zhubaowz.html" title="珠宝高端奢侈品网站建设解决方案">珠宝高端奢侈品网站建设解决方案</a></li> </ul> </div> <div class="links w2"> <h2 class="h2">我们的实力<a href="/about/" title="更多" class="more">更多+</a></h2> <ul> <li>10年专业互联网服务经验</li> <li>成都高端建站设计团队</li> <li>资深行业分析策划</li> <li>B2C营销型网站建设者</li> <li>前沿视觉设计、研发能力</li> <li>前端代码深度符合SEO优化</li> <li>成都市高新技术企业证书</li> <li>具有完备的项目管理</li> <li>完善的售后服务体系</li> <li>深厚的网络运营经验</li> <li>时刻新技术研发能力</li> <li>16个网站系统软件著作权</li> </ul> </div> <div class="f_div2_r"> <h2 class="h2">关于甜橘子<a href="/about/" title="更多" class="more">更多+</a></h2> 甜橘子网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、行业门户型、品牌建立型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,甜橘子网站制作为客户规划、定制符合企业需求、带有营销价值的建站方案,提供从网站前期定位分析策划到网站界面设计... </div> <div class="c_l"></div> </div> <div class="wrap"> <div class="f_div3"> <span class="l">成都网站制作案例©2020 甜橘子设计 版权所有 | <a href="http://chengdu.cdxwcx.cn" target="_blank">甜橘子网站设计</a><a href="http://chengdu.cdxwcx.cn" target="_blank">chengdu.cdxwcx.cn</a></span> <span class="r"><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备11025516号</a></span> </div> </div> </div> </footer> <!--尾部end--> <script language="javascript" src="/Public/Home/js/foot.js"></script> <!--尾部end--> <!--侧边栏begin--> <div class="side"> <ul> <li id="qqonline_xbceo"><a href="tencent://message/?uin=631063699&Site=&Menu=yes"><i class="bgs1"></i>QQ咨询</a></li> <li class="shangqiao"><a href="tencent://message/?uin=532337155&Site=&Menu=yes" title="在线咨询"> <div><i class="bgs2"></i>在线咨询</div> </a></li> <li class="sideewm"><i class="bgs3"></i>官方微信 <div class="ewBox"></div> </li> <li class="sideetel"><i class="bgs4"></i>联系电话 <div class="telBox"> <dd class="bgs1"><span>座机</span><a href="tel:028-86922220" target="_blank">028-86922220</a></dd> <dd class="bgs2"><span>手机</span><a href="tel:13518219792" target="_blank">13518219792</a></dd> </div> </li> <li class="sidetop" onClick="goTop()" id="sidetop"><i class="bgs6"></i>返回顶部</li> </ul> </div> <script type="text/javascript"> $('.sideewm').hover(function(){ $('.ewBox').stop().fadeIn(); },function(){ $('.ewBox').stop().fadeOut(); }); $('.sideetel').hover(function(){ $('.telBox').stop().fadeIn(); },function(){ $('.telBox').stop().fadeOut(); }); $(".con_id 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); } }); </script> <!-- WPA start --> <!-- WPA end --> <!--侧边栏end--> </body> </html>