实现多附件上传的三种方法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Web_MacAdmin_Test" %>
<!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>无标题页</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>
后台
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());
2. 第二种方式是存贮在服务器端,创建一个临时的Table,将上传的文件转换为二进制数据,然后通过二进制文件保存到服务器端。
protected void Button1_Click(object sender, EventArgs e)
{
//Button1.Attributes.Add("onclick", "re();")
FileUpload file = new FileUpload();
file = FileUpload1;
DataTable dt = (DataTable)ViewState["Table"];
DataRow dr=dt.NewRow();
int imageDataLen = file.PostedFile.ContentLength;
byte[] imageData = new byte[imageDataLen];
Stream imageStream = file.PostedFile.InputStream;
int n = imageStream.Read(imageData, 0, imageDataLen);
// imageStream.Dispose();
//imageStream.Close();
dr["aa"] = imageData;
dr["bb"] = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(@"\") + 1);
dt.Rows.Add(dr);
Response.Write("<script>window.opener.location.href='default2.aspx'</script>");
ViewState["Table"] = dt;
Response.Write(dt.Rows.Count);
//this.GridView1.DataSource = dt;
//this.GridView1.DataBind();
}
private void Create()
{
DataTable dt = new DataTable();
dt.Columns.Add("aa",typeof(byte[]));
dt.Columns.Add("bb", typeof(string));
ViewState["Table"] = dt;
}
private void WriteToFile(string strPath, ref byte[] Buffer)
{
// Create a file
FileStream newFile = new FileStream(strPath, FileMode.Create);
// Write data to the file
newFile.Write(Buffer, 0, Buffer.Length);
// Close file
newFile.Close();
}
第三种方法是直接在服务器端存贮个fileupload对象,需要提醒的是,由于viewstate只能保存list\array等类型,不能保存fileupload类型,就必须用session来存贮。
posted on 2008-06-30 18:51 Love Fendi 阅读(1976) 评论(2) 编辑 收藏 举报