页面文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpLoad.aspx.cs" Inherits="up_UpLoad" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var i = 1;
function show()
{
if(i >= 5)
{
alert("提示:最多只能上传4个附件!");
return false;
}
var file = document.createElement("input");
file.type = "file";
file.id = file.name = "file" + i;
document.getElementById("div").appendChild(file);
var btn = document.createElement("input");
btn.value = "删除";
btn.type = "button";
btn.id = btn.name = "btn" + i;
btn.onclick = function()
{
document.getElementById("div").removeChild(file);
document.getElementById("div").removeChild(btn);
document.getElementById("div").removeChild(br);
i--;
}
document.getElementById("div").appendChild(btn);
var br = document.createElement("br");
document.getElementById("div").appendChild(br);
i++;
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data" >
<div>
<asp:Button ID="Button1" runat="server" Text="UpLoad" OnClick="Button1_Click" /><br /><br />
<div id = 'div'></div>
<input type="button" οnclick="show()" value="添 加">
</div>
</form>
</body>
</html>
CS文件
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i] as HttpPostedFile;
string fileName = Path.GetFileName(file.FileName);
string path = Server.MapPath("File/");
// 保存
file.SaveAs(path + fileName);
}