<%@ Page Language="C#" AutoEventWireup="true" CodeFile="批量上传文件.aspx.cs" Inherits="批量上传文件" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div style="width:300px; margin:50px auto;">
<asp:Label ID="LblMessage" runat="server" Width="300px" ForeColor="#FF0033" Font-Bold="True" Font-Size="Small" />
<table border="1" bordercolor="gray" style="border-collapse: collapse;">
<tr>
<td style="text-align: center; font-size:10pt; font-weight:bold; color:DimGray;">
批量上传文件
</td>
</tr>
<tr>
<td>
<asp:Panel ID="Pan_UpFile" runat="server" Height="200px" ScrollBars="Auto" Width="250px">
<table id="Tab_UpDownFile" runat="server" cellpadding="0" cellspacing="0" enableviewstate="true">
<tr>
<td style="width: 100px; height: 30px">
<asp:FileUpload ID="FileUpload1" runat="server"/>
</td>
</tr>
</table>
</asp:Panel>
</td>
</tr>
<tr>
<td>
<asp:Button ID="BtnAdd" runat="server" Text="添加文件" OnClick="BtnAdd_Click" BorderColor="Gray" BorderWidth="1px" />
<asp:Button ID="BtnUpFile" runat="server" OnClick="BtnUpFile_Click" Text="上传文件" BorderColor="Gray" BorderWidth="1px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台批量上传文件.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI.HtmlControls;
public partial class 批量上传文件 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SFUPC();
}
}
#region 该方法是将页面中的上传文件的控件保存到session中
private void SFUPC()
{
//声明一个ArrayList用于存放上传文件的控件
ArrayList AL = new ArrayList();
foreach (Control C in this.Tab_UpDownFile.Controls)
{
if (C.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell HTC = (HtmlTableCell) C.Controls[0];
foreach (Control FUC in HTC.Controls)
{
if (FUC.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
FileUpload FU = (FileUpload)FUC;
FU.BorderColor = System.Drawing.Color.DimGray;
FU.BorderWidth = 1;
AL.Add(FU);
}
}
}
}
Session.Add("FilesControls", AL);
}
#endregion
#region 该方法用于添加一个上传文件的控件
private void InsertC()
{
//实例化一个ArrayList
ArrayList AL = new ArrayList();
//清除表里的所有行
this.Tab_UpDownFile.Rows.Clear();
//获得Session里存放的上传文件的控件
GetInfo();
//实例化表格的行
HtmlTableRow HTR = new HtmlTableRow();
//实例化表格的列
HtmlTableCell HTC = new HtmlTableCell();
//向列里添加上传控件
HTC.Controls.Add(new FileUpload());
HTR.Controls.Add(HTC);
this.Tab_UpDownFile.Rows.Add(HTR);
SFUPC();
}
#endregion
#region 该方法将session中的上传控件集添加的表格中
private void GetInfo()
{
ArrayList AL = new ArrayList();
if (Session["FilesControls"] != null)
{
AL=(ArrayList)Session["FilesControls"];
foreach (FileUpload FU in AL)
{
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add(FU);
HTR.Controls.Add(HTC);
this.Tab_UpDownFile.Rows.Add(HTR);
}
}
}
#endregion
#region 该方法用于执行文件上传操作
private void UpFile()
{
//获取文件夹路径
string filepath = Server.MapPath("./") + "UpFile";
//获取客服端上载文件的集合
HttpFileCollection HFC = Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile UserHPF =(HttpPostedFile) HFC[i];
try
{
if (UserHPF.ContentLength > 0)
{
UserHPF.SaveAs(filepath+"\\"+System.IO.Path.GetFileName(UserHPF.FileName));
}
}catch
{
this.LblMessage.Text="上传失败!";
}
}
if(Session["FilesControls"]!=null)
{
Session.Remove("FILEsCOntrols");
}
this.LblMessage.Text="上传成功!";
}
#endregion
protected void BtnAdd_Click(object sender, EventArgs e)
{
InsertC();
this.LblMessage.Text = "";
}
protected void BtnUpFile_Click(object sender, EventArgs e)
{
if (this.FileUpload1.PostedFile.FileName != null)
{
UpFile();
SFUPC();
}
else
{
this.LblMessage.Text = "对不起!上传文件不能为空";
}
}
}
效果图: