最终效果如下:
现贴出核心代码如下:
aspx里的代码:
复制代码 代码如下:
在添加文件和删除文件里调用了Javascript,代码如下:
复制代码 代码如下:
后台响应保存文件的操作,保存文件关键的一句是要读取到文件列表,
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
上传以后保存文件的代码如下:
复制代码 代码如下:
protected void btnUpload_Click(object sender, EventArgs e)
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Text.StringBuilder strMsg = new StringBuilder("
");
strMsg.Append("上传的文件分别是:");
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);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "
");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "
");
strMsg.Append("上传文件的文件名:" + fileName + "
");
strMsg.Append("上传文件的扩展名:" + fileExtension + "
");
strMsg.Append("上传文件的大小:" + postedFile.ContentLength + "
");
//可扩展功能:
//保存文件时可以设置保存目录
//可以重命名文件保存
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}
完整代码下载



