同时上传多文件问题
[color=Red]页面代码:[/color]<%@ 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>
[color=Red]CS代码:[/color]
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 = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != String.Empty)
{
//取得文件的扩展名
fileExtension = System.IO.Path.GetExtension(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 = "上传失败";
}
}
int countFiles = Request.Files.Count;
for(int i = 0; i < countFiles; i++)
{
HttpPostedFile hpf = Request.Files[i];
.....
}在上面判断路径的时候要注意""的内容,即有些上传不选,如果不加判断会出现上传能上传但页面会出现路径找不到的问题,这里可以使用一个方式,如:
if(hpf.ContentLength != 0)来判断,ContentLength代表着上传内容的大小,以字节计,具体的可以参考MSDN
在html元素内form的html控件必须带Enctype = "multipart/form-data",否则会出现上传不完整
如:
<form id = "myForm" runat = "server" Enctype = ""multipart/form-data">
<input type = "file" id = "fileUpload1" runat = "server" />
<input type = "file" id = "fileUpload2" runat = "server" />
<input type = "file" id = "fileUpload3" runat = "server" />
.......
[tk09]
[[it] 本帖最后由 Xxibug 于 2008-8-29 09:13 编辑 [/it]]
页:
[1]
