用HttpFileCollection实现多文件上传的例子
注意:页面Form的属性一定要设为enctype="multipart/form-data"
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upLoadFiles.aspx.cs" Inherits="NetSamples.upLoadFiles" %>

<!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>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<input id="File1" type="file" name="File1" />
<br />
<input id="File2" type="file" name="File2" />
<br />
<asp:Button
ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


namespace NetSamples
{
public partial class upLoadFiles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Files != null)
{
HttpFileCollection files = Request.Files;
string fileName;
for(int i = 0;i < files.Count;i++)
{
if (files[i].ContentLength <= 0)
{
continue;
}
fileName = Path.GetFileName(files[i].FileName);
files[i].SaveAs(Request.MapPath(@"../files/" + fileName));
}
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upLoadFiles.aspx.cs" Inherits="NetSamples.upLoadFiles" %>
<!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>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<input id="File1" type="file" name="File1" />
<br />
<input id="File2" type="file" name="File2" />
<br />
<asp:Button
ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace NetSamples
{
public partial class upLoadFiles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Files != null)
{
HttpFileCollection files = Request.Files;
string fileName;
for(int i = 0;i < files.Count;i++)
{
if (files[i].ContentLength <= 0)
{
continue;
}
fileName = Path.GetFileName(files[i].FileName);
files[i].SaveAs(Request.MapPath(@"../files/" + fileName));
}
}
}
}
}
获取文件扩展名:System.IO.Path.GetExtension(path);
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.


浙公网安备 33010602011771号