ASPX页:
<head runat="server">
<title>多文件上传</title>
<script type="text/javascript" language="javascript">
function addForm()
{
var strForm ="<input type='file' size='50' name='File' />"
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",strForm)
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<p id="MyFile">
<input type="file" size="50" name="File" />
</p>
<p>
<input type="button" value="增加一个" onclick="addForm()" />
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"
onclick="UploadButton_Click">
</asp:Button>
<input onclick="this.form.reset()" type="button" value="重 置" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
cs页:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _8_02 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UploadButton_Click(object sender, EventArgs e)
{
this.SaveFiles();
}
private void SaveFiles()
{
//遍历表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//状态信息
string strout = "<br>上传的文件分别是:<hr color=red><table style='width: 500px;'>";
strout += "<tr><td>文件类型</td><td>客户端地址</td><td>上传文件名</td><td>扩展名</td></tr>";
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//访问单独文件
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strout += "<tr><td>" + postedFile.ContentType.ToString() + "</td>";
strout += "<td>" + postedFile.FileName + "</td>";
strout += "<td>" + fileName + "</td>";
strout += "<td>" + fileExtension + "</td></tr>";
postedFile.SaveAs(Server.MapPath("uploadFile/") + fileName);
}
else
{
strout = "<br>请您选择一个文件!!!";
}
}
Label1.Text = strout.ToString();
}
catch (Exception Ex)
{
Label1.Text = Ex.Message.ToString();
}
}
}