asp.net中实现文件批量上传
生活随笔
收集整理的這篇文章主要介紹了
asp.net中实现文件批量上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天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(){//實例化ArrayListArrayList 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}總結
以上是生活随笔為你收集整理的asp.net中实现文件批量上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天玑820相当于骁龙什么处理器 天玑10
- 下一篇: 什么是买盘