您的位置:首页 > 编程语言 > ASP

asp.net,c#,同时上传多个文件

2011-08-19 10:52 429 查看
ASPX页:

<head runat="server">

<title>多文件上传</title>

<script type="text/javascript" language="javascript">

function addForm()

{

var strForm ="<input type='file' size='50' name='File' />"

document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",strForm)

}

</script>

</head>

<body>

<form id="form1" runat="server" enctype="multipart/form-data">

<p id="MyFile">

<input type="file" size="50" name="File" />

</p>

<p>

<input type="button" value="增加一个" onclick="addForm()" />

<asp:Button Runat="server" Text="开始上传" ID="UploadButton"

onclick="UploadButton_Click">

</asp:Button>

<input onclick="this.form.reset()" type="button" value="重 置" />

<br />

<asp:Label ID="Label1" runat="server"></asp:Label>

</p>

</form>

</body>

</html>

cs页:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

public partial class _8_02 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void UploadButton_Click(object sender, EventArgs e)

{

this.SaveFiles();

}

private void SaveFiles()

{

//遍历表单元素

HttpFileCollection files = HttpContext.Current.Request.Files;

//状态信息

string strout = "<br>上传的文件分别是:<hr color=red><table style='width: 500px;'>";

strout += "<tr><td>文件类型</td><td>客户端地址</td><td>上传文件名</td><td>扩展名</td></tr>";

try

{

for (int iFile = 0; iFile < files.Count; iFile++)

{

//访问单独文件

HttpPostedFile postedFile = files[iFile];

string fileName, fileExtension;

fileName = System.IO.Path.GetFileName(postedFile.FileName);

if (fileName != "")

{

fileExtension = System.IO.Path.GetExtension(fileName);

strout += "<tr><td>" + postedFile.ContentType.ToString() + "</td>";

strout += "<td>" + postedFile.FileName + "</td>";

strout += "<td>" + fileName + "</td>";

strout += "<td>" + fileExtension + "</td></tr>";

postedFile.SaveAs(Server.MapPath("uploadFile/") + fileName);

}

else

{

strout = "<br>请您选择一个文件!!!";

}

}

Label1.Text = strout.ToString();

}

catch (Exception Ex)

{

Label1.Text = Ex.Message.ToString();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: