现在的位置: 首页 > 综合 > 正文

多文件上传

2013年08月04日 ⁄ 综合 ⁄ 共 2587字 ⁄ 字号 评论关闭

<%@ Page language="c#" Codebehind="MultiAttchments.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.MultiAttchments" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<script>
function AddAttachments()
{
document.getElementById('attach').innerText = "继续添加附件";

tb = document.getElementById('attAchments');
newRow = tb.insertRow();
newRow.insertCell().innerHTML = "<input name='File' size='50' type='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 MS_POSITIONING="GridLayout">
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div><table id="attAchments"></table></div><span><IMG src="icoAddFl.gif"> </span> <A id="attach" style="font-family:宋体;font-size:9pt;" title="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件总量不能超过发送限制的大小。" onclick="AddAttachments();"
href="javascript:;" name="attach">添加附件</A>
<br/><br/><br/><br/><br/><br/>
<asp:Button id="btnSend" runat="server" Text=" 上传 "></asp:Button>
</form>
</body>
</HTML>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace WebApplication3
{
/// <summary>
/// Summary description for WebForm7.
/// </summary>
public class MultiAttchments : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnSend;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{   
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnSend_Click(object sender, System.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());
}
}
}

 

抱歉!评论已关闭.