62,046
社区成员
发帖
与我相关
我的任务
分享
<head id="Head1" runat="server">
<title>无标题页</title>
<script type="text/javascript">
function addFileUpLoad(){
var str = '<INPUT type="file" size="43" NAME="File">';
document.getElementById('addFileupLoad').insertAdjacentHTML("beforeEnd",str);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="addFileupLoad" style="width: 436px;">
<asp:Label ID="Label1" runat="server" Text="点此增加文件" Width="68px"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" /><br />
<asp:FileUpload ID="FileUpload1" runat="server" Width="363px" /><br />
<asp:FileUpload ID="FileUpload2" runat="server" Width="363px" /><br />
</div>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
Label1.Attributes.Add("onclick", "addFileUpLoad()");
}
protected void Button1_Click(object sender, EventArgs e)
{
bool bo = Save();
if (bo)
{
Response.Write("<script language='javaScript'>alert('上传文件成功!')</script>");
}
}
private Boolean Save()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = null;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
String path = Server.MapPath("~/File/");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
postedFile.SaveAs(path + fileName);
}
}
return true;
}
catch (Exception e)
{
e.ToString();
return false;
}
}