前台
<script language="javascript">
function addFileControl()
{
var str='<input type=file name=File>'
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="uoMoreFile" method="post" runat="server" enctype="multipart/form-data">
<asp:Label Runat="server" ID="Title"></asp:Label>
<p id="FileCollection"><input type="file" size="50" name="File"></p>
<p>
<input type="button" value="增加(File)" onclick="addFileControl()">
<asp:Button Runat="server" Text="上传" id="Upload"></asp:Button>
<input onclick="this.Form.reset()" type="button" value="重置"></p>
<p align="center"><asp:Label ID="strStatus" Runat="server" Width="500px"></asp:Label></p>
</form>
</body>
后台CS
protected System.Web.UI.WebControls.Label Title;
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.EventArgs e)
{
Title.Text="<h3>多文件上传</h3>";
// 在此处放置用户代码以初始化页面
}
private void Upload_Click(object sender, System.EventArgs e)
{
upMorefile();
}
private void upMorefile()
{
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("upedFile\\")+fileName);
}
}
strStatus.Text=strMsg.ToString();
return true;
}
catch(System.Exception error)
{
strStatus.Text=error.ToString();
return false;
}
}