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= " 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= " 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 }
完!
请读者根据实际情况做适当修改!
分类: Asp.net
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did48817