多附件上传
前台:
在<head></head>中加入下段javascript
<script language="JavaScript">
function addFileControl()
{
var str = '<br><INPUT type="file" NAME="File" style="WIDTH: 428px; HEIGHT: 22px" > '
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
}
</script>
页面中加入下列控件还有一个web控件button
其中在的html代码中 "增加"处加上 onclick="addFileControl()"
“<INPUT onclick="addFileControl()" type="button" value="增加(File)">”
后台代码
private void Upload_Click(object sender, System.EventArgs e)
{
string path="";定义上传目录
if(System.IO.Directory.Exists(Server.MapPath(path))==true)
{
try
{
string s=path+"/";
//调用upMorefile
upMorefile(s);
}
catch(Exception err)
{
Response.Write("<script>alert('"+err.Message+"');</script>");
}
}
else
{
Response.Write("<script>alert('不存在该目录,无法上传,请在修改模块中增加!');</script>");
}
}
//上传文件
private void upMorefile(string paths)
{
//遍历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(paths) + fileName);
}
}
strStatus.Text = strMsg.ToString();
// return true;
}
catch(System.Exception error)
{
strStatus.Text = error.Message;
// return false;
}
}