• asp.net中实现文件批量上传


    前台批量上传文件.aspx

    <%@ 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="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>

    后台批量上传文件.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 = "对不起!上传文件不能为空";
            }
        }
    }

    效果图:

  • 相关阅读:
    Java实现“睡排序”——线程池Executors的使用
    浅谈HashMap与线程安全 (JDK1.8)
    Ubuntu 16 Java Develop环境快速搭建
    Spring Boot在反序列化过程中:jackson.databind.exc.InvalidDefinitionException cannot deserialize from Object value
    Java 8 – Map排序
    vue指令优化网络图片加载速度
    如何实现小于12px的字体效果
    两种以上方式实现已知或者未知宽度的垂直水平居中
    C# winform窗体间传值(使用委托或事件)
    C#栈Stack的使用
  • 原文地址:https://www.cnblogs.com/zcttxs/p/2549939.html
Copyright © 2020-2023  润新知