今天BOSS要求做一個批量上傳文件的功能,忙活了半天,總算搞定,希望前輩們多加指點,下面來看一下效果圖(這里是簡化版,只介紹了主要實現過程,沒有美化,勿怪!勿怪!):

單擊添加文件,將自動添加FileUpload控件。

單擊瀏覽分別選擇要上傳的多個文件

單擊上傳文件,完成文件的上傳。

好了不多說,下面是代碼:
前台Default.aspx代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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> </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>
Default.aspx.cs代碼(注釋有一定說明,都能看懂吧?)
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections;//引入命名空間 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack)//首次執行頁面 { SFUPC(); } } #region 該方法用於將當前頁面上傳文件控件集保存到Session中 private void SFUPC() { ArrayList AL = new ArrayList();//動態增加數組 foreach (Control C in Tab_UpDownFile.Controls) { //在表格中查找出FileUpload控件添加到ArrayList中 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; //設置FileUpload的樣式 FU.BorderColor = System.Drawing.Color.DimGray; FU.BorderWidth = 1; //添加FileUpload控件 AL.Add(FU); } } } } //把ArrayList添加到Session中 Session.Add("FilesControls", AL); } #endregion #region 該方法用於添加一個上傳文件的控件 private void InsertC() { //實例化ArrayList ArrayList AL = new ArrayList(); this.Tab_UpDownFile.Rows.Clear(); //清除id為F表格里的所有行 GetInfo(); //表示 HtmlTable 控件中的 <tr> HTML 元素 HtmlTableRow HTR = new HtmlTableRow(); //表示 HtmlTableRow 對象中的 <td> 和 <th> HTML 元素 HtmlTableCell HTC = new HtmlTableCell(); //在單元格中添加一個FileUpload控件 HTC.Controls.Add(new FileUpload()); //在行中添加單元格 HTR.Controls.Add(HTC); //在表中添加行 Tab_UpDownFile.Rows.Add(HTR); SFUPC(); } #endregion #region 該方法用於將保存在Session中的上傳文件控件集添加到表格中 private void GetInfo() { ArrayList AL = new ArrayList(); if (Session["FilesControls"] != null) { AL = (ArrayList)Session["FilesControls"]; for (int i = 0; i < AL.Count; i++) { HtmlTableRow HTR = new HtmlTableRow(); HtmlTableCell HTC = new HtmlTableCell(); HTC.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]); HTR.Controls.Add(HTC); Tab_UpDownFile.Rows.Add(HTR); } } } #endregion #region 該方法用於執行文件上傳操作 private void UpFile() { //獲取文件夾路徑 string FilePath = Server.MapPath("./") + "File"; // 獲取客戶端上載文件的集合 HttpFileCollection HFC = Request.Files; for (int i = 0; i < HFC.Count; i++) { //訪問指定的文件 HttpPostedFile UserHPF = HFC[i]; try { //判斷文件是否為空 if (UserHPF.ContentLength > 0) { //將上傳的文件存儲在指定目錄下 UserHPF.SaveAs(FilePath + "\\" + System.IO.Path.GetFileName(UserHPF.FileName)); } } catch { LblMessage.Text = "上傳失敗!"; } } if (Session["FilesControls"] != null) { Session.Remove("FilesControls"); } LblMessage.Text = "上傳成功!"; } #endregion #region 調用InsertC方法,實現添加FileUpLoad控件的功能 protected void BtnAdd_Click(object sender, EventArgs e) { InsertC();//執行添加控件方法 LblMessage.Text = ""; } #endregion #region 實現文件上傳的功能 protected void BtnUpFile_Click(object sender, EventArgs e) { if (this.FileUpload1.PostedFile.FileName != "") { UpFile();//執行上傳文件 SFUPC(); } else { LblMessage.Text = "對不起,上傳文件為空,請選擇上傳文件!"; } } #endregion }
完!
請讀者根據實際情況做適當修改!
