Asp.net动态添加附件上传

ExpandedBlockStart.gif 代码
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " WebForm1.aspx.cs "  Inherits = " ClassifyInfo.WebForm1 "   %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title ></ title >
    
< script  type ="text/javascript" >
        
function  AddAttachments() {
            document.getElementById(
" attach " ).innerText  =   " 继续添加附件 " ;
            
// tb变量未加var在javascript中代表全局变量
            tb  =  document.getElementById( " attachments " );
            
var  newRow  =  tb.insertRow();
            newRow.insertCell().innerHTML 
=   " <input type='file' size='50' name='File' />&nbsp;&nbsp;<input type='button' value='删除' οnclick='delFile(this.parentElement.parentElement.rowIndex)' /> " ;
        }

        
function  delFile(index) {
            document.getElementById(
" attachments " ).deleteRow(index);
            tb.rows.length 
>   0   ?  document.getElementById( ' attach ' ).innerText  =   " 继续添加附件 "  : document.getElementById( " attach " ).innerText  =   " 添加附件 " ;
        }
    
</ script >
</ head >
< body >
    
< form  id ="form1"  runat ="server"   enctype ="multipart/form-data"   method ="post" >
    
< div >
    
< table   id ="attachments" ></ table >
    
< id ="attach"  name ="attach"  href ="javascript:;"  style ="font-family:宋体; font-size:9pt;"  title ="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件总量不能超过发送限制的大小"  onclick ="AddAttachments()" > 添加附件 </ a >
    
< br  />< br  />< br  />< br  />< br  />
        
< asp:Button  ID ="btnSend"  runat ="server"  Text ="上传"  onclick ="btnSend_Click"   />
    
</ div >
    
</ form >
</ body >
</ html >

 

ExpandedBlockStart.gif 代码
  protected   void  btnSend_Click( object  sender, EventArgs e)
        {
            StringBuilder sb 
=   new  StringBuilder();

            
int  attCount  =   0 ;

            
string  filePath  =   "" ;

            
for  ( int  i  =   0 ; i  <  Request.Files.Count; i ++ )
            {
                
if  (Request.Files[i].ContentLength  >   0 )
                {
                    filePath 
=  Request.Files[i].FileName;
                    sb.Append(
" Files "   +  ++attCount   +   " "   +  filePath  +   " <br> " );
                    Request.Files[
0 ].SaveAs(Server.MapPath( " ./ " +  filePath.Substring(filePath.LastIndexOf( " \\ " +   1 ));
                }
            }

            sb.Insert(
0 " you upload  "   +  attCount  +   "  files.<br> " );

            Response.Write(sb.ToString());


        }

 

 

ExpandedBlockStart.gif 代码
< system.web >
        
< httpRuntime  maxRequestLength ="10240"  executionTimeout ="45"  useFullyQualifiedRedirectUrl ="true" />
        
<!--  
        maxRequestLength:指定输入流缓冲阈值限制(以 KB 为单位),最大上传文件大小
        executionTimeout:指定在被 ASP.NET 自动关闭前,允许执行请求的最大秒数。
        useFullyQualifiedRedirectUrl:指定客户端重定向是否是完全限定的(采用 "http://server/path" 形式,这是某些移动控件所必需的)
        
-->
</ system.web >

 

 

转载于:https://www.cnblogs.com/jhxk/articles/1759776.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值