可以动态的增加控件,多文件上传
MultiUpload.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>创建项目</title>
<link href="../css.css" rel="stylesheet" type="text/css" />
</head>
<script language="JavaScript">
function addFile()
{
var str = '<br /><br />中文名:<INPUT type="text" style="height:14px;" NAME="Text"> <INPUT type="file" size="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
<body>
<form id="Form1" runat="server" method="post" enctype="multipart/form-data">
<uc1:Header ID="Header1" runat="server" />
<table width="1004" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="92" style="background:url(../images/index-016.jpg);">
</td>
<td style="height:500px; vertical-align:top;">
<br />
<table width="95%" border="0" cellspacing="1" cellpadding="0" align="center" bgcolor="gray">
<tr>
<td bgcolor="#F3F3F3" height="30"> <font color="red"><strong><asp:Label ID="labTitle" runat="server"></asp:Label></strong></font></td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="30">上传附件</td>
</tr>
<tr>
<td>
<P id="MyFile">中文名:<INPUT type="text" style="height:14px;" NAME="Text"> <INPUT type="file" size="50" NAME="File"></P>
</td>
</tr>
<tr>
<td height="50">
<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>
<br />
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</td>
</tr>
</table>
</td>
<td width="66" style="background:url(../images/index-015.jpg);"> </td>
</tr>
</table>
</form>
</body>
</html>
===============================================================================
MultiUpload.aspx.cs
/// <summary>
/// 点击上传文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UploadButton_Click(object sender, EventArgs e)
{
ProjectFile proFile = new ProjectFile();
proFile.ProjectId = proId;
proFile.FileType = typeId;
//文件路径
string categoryPath=string.Empty;
if (typeId == 1) //背景资料
{
categoryPath = "File_background";
}
else if (typeId == 2)//提案资料
{
categoryPath = "File_proposal";
}
else if (typeId == 3)//结案报告
{
categoryPath = "File_endcase";
}
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//遍历File表单元素(返回的是字符串的形式,eg:25,30.60-----分割以后取值赋值给数组)
string[] text = HttpContext.Current.Request.Form["Text"].ToString().Split(',');
//状态信息
StringBuilder strMsg = new StringBuilder();
strMsg.Append("<br /><br />上传的文件分别是:<hr color=red>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension, file_id;
//取text表单的值
string textName = text[iFile];
//取出精确到毫秒的时间做文件的名称
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day = System.DateTime.Now.Day;
int hour = System.DateTime.Now.Hour;
int minute = System.DateTime.Now.Minute;
int second = System.DateTime.Now.Second;
int millisecond = System.DateTime.Now.Millisecond;
string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString() + iFile.ToString();
fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
file_id = my_file_id + fileExtension;
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + file_id + "<br>");
strMsg.Append("中文文件名:" + textName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(categoryPath + "/") + file_id);
//对象赋值
proFile.FileName = textName;
proFile.FilePath = @"~/Project/" + categoryPath + "/" + file_id;
proFile.InsertTime = DateTime.Now;
ServiceLocator.ProjectService.InsertProjectFile(proFile);
}
}
strStatus.Text = strMsg.ToString();
JavaScript.Alert("恭喜您!上传成功!", this.Page);
DataBind_File(proId, typeId);
return;
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}