前台页面:
JS:
<script language="javascript" type="text/javascript">
function addFile() {
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById("MyFile").insertAdjacentHTML("beforeEnd", str);
var img = '<img id="Image1" alt="" />'
document.getElementById("MyImg").insertAdiacentHTML("beforeEnd", img);
}
function show(id) {
document.getElementById("Image1").src = id;
}
</script>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>
多文件上传</h3>
<p id="MyImg">
<img id="Image1" alt="" />
</p>
<p id="MyFile">
<input type="file" size="50" name="File" onpropertychange="show(this.value)"/></p>
<p>
<input type="button" value="增加(Add)" οnclick="addFile()" />
<input οnclick="this.form.reset()" type="button" value="重置(ReSet)" />
<asp:Button runat="server" Text="开始上传" ID="UploadButton" OnClick="UploadButton_Click">
</asp:Button>
</p>
<p>
<%=strMsg %>
</p>
</div>
</form>
后台程序:
protected void UploadButton_Click(object sender, EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
strMsg.Append("上传的文件分别是:<br>");
for (int iFile = 0; iFile < files.Count; iFile++)
{
HttpPostedFile postFile = files[iFile];
string fileName, fileExtention;
fileName = Path.GetFileName(postFile.FileName);//获取文件名
if (fileName != null)
{
fileExtention = Path.GetExtension(fileName);
string filePath = System.Web.HttpContext.Current.Request.MapPath("upfile/");//要保存的文件路径
strMsg.Append("上传类型:" + postFile.ContentType.ToString() + "<br>");//文件类型
strMsg.Append("客户端文件地址:" + postFile.FileName + "<br>");//上传的文件地址
strMsg.Append("上传文件的扩展名:" + fileExtention + "<br>");//文件扩展名
strMsg.Append("上传文件大小:"+postFile.ContentLength.ToString()+"<br>");//文件大小
strMsg.Append("上传地址:" + filePath + fileName);
postFile.SaveAs(filePath + fileName);
}
}
}