Asp.net动态添加附件上传

代码
<%@ 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='删除' onclick='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>

 

代码
 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());


        }

 

 

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

 

 

posted @ 2010-06-17 21:29  唔愛吃蘋果  阅读(2802)  评论(0编辑  收藏  举报