javascript 上传附件
<form id="form2" method="post" runat="server" enctype="multipart/form-data">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function AddAttachments()
{
document.getElementById('attach').innerText = "继续添加附件";
tb = document.getElementById('attAchments');
newRow = tb.insertRow();
newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'> <input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";
}
function delFile(index)
{
document.getElementById('attAchments').deleteRow(index);
tb.rows.length > 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').innerText = "添加附件";
}
</script>
</head>
<body>
<form id="form2" method="post" runat="server" enctype="multipart/form-data">
<div>
<table id="attAchments">
</table>
</div>
<span>
</span><a id="attach" style="font-family: 宋体; font-size: 9pt;" title="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件总量不能超过发送限制的大小。"
onclick="AddAttachments();" href="javascript:;" name="attach">添加附件</a>
<asp:Button ID="btnSend" runat="server" Text=" 上传 " OnClick="btnSend_Click"></asp:Button>
</form>
</body>
</html>
后台代码
protected void btnSend_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
int attCount = 0;
string filePath = "";
for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].ContentLength > 0)
{
filePath = Request.Files[i].FileName;
sb.Append("Files" + attCount++ + ": " + filePath + "<br>");
Request.Files[0].SaveAs(Server.MapPath("./") + filePath.Substring(filePath.LastIndexOf("\\") + 1));
}
}
sb.Insert(0, "you upload " + attCount + " files.<br>");
Response.Write(sb.ToString());
}

浙公网安备 33010602011771号