同时上传多文件问题
页面代码:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="uploadmorefile.ascx.cs" Inherits="controls_upload" %><script type="text/javascript">
function addFileControl()
{
var str = '<br/><INPUT type="file" NAME="File" id="File">'
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
}
</script>
<table width="500" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td height="25" colspan="3">
</td>
</tr>
<tr>
<td height="25" colspan="3">
<P id="FileCollection"><INPUT type="file" name="File" id="File">
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label></P></td>
</tr>
<tr>
<td width="200" height="25">
<input name="button" type="button" onclick="addFileControl()" value="增加(File)"></td>
<td width="200">
<asp:Button ID="Upload" runat="server" Text="上传" Width="110px" OnClick="Upload_Click">
</asp:Button></td>
<td width="200">
<input name="button2" type="button" style="width: 56px; height: 24px" onclick="this.form.reset()"
value="重置"></td>
</tr>
<tr>
<td height="25" colspan="3">
<asp:Label ID="strStatus" runat="server" BorderColor="White" BorderStyle="None" Width="500px"
Font-Size="9pt" Font-Bold="True" Font-Names="宋体"></asp:Label></td>
</tr>
</table>
CS代码:
private bool upMorefile()
{
//遍历File表单元素
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
int fileCount;
int filecount = files.Count;
try
{
for (fileCount = 0; fileCount < files.Count; fileCount++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile = files[fileCount];
string fileName, fileExtension;
//取得上传得文件名
fileName = (postedFile.FileName);
if (fileName != String.Empty)
{
//取得文件的扩展名
fileExtension = (fileName);
//上传的文件信息
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
//保存到指定的文件夹
postedFile.SaveAs(Server.MapPath("map/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch (System.Exception error)
{
strStatus.Text = error.Message;
return false;
}
}
protected void Upload_Click(object sender, EventArgs e)
{
if (upMorefile())
{
Label1.Text = "上传成功";
}
else
{
Label1.Text = "上传失败";
}
}