.NET多文件批量上传

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpLoad.aspx.cs" Inherits="UpLoad" %>

<!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>
    <script language="JavaScript">
    function addFile()
    {
     var str = '<INPUT type="file" size="50" NAME="File">'
     document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
    }
    </script>
  </HEAD>
<body>
    <form id="form1" runat="server">
  <div align="center">
        <h3>多文件上传</h3>
      <p>
          <input id="HIF" type="file" runat="server" />&nbsp;</p>
      <p>
          <asp:ListBox ID="FileList" runat="server" Width="201px"></asp:ListBox>&nbsp;</p>
      <p>
          <asp:Button ID="AddFile" runat="server" Text="添加文件" OnClick="AddFile_Click" />
          <asp:Button ID="RemvFile" runat="server" Text="删除文件" OnClick="RemvFile_Click" />
          <input id="Uploads" type="submit" value="上传" runat="server" onserverclick="Uploads_ServerClick" />
      </p>
      <p>
          <asp:Label ID="TipInfo" runat="server"></asp:Label>&nbsp;</p>
      </div>

    </form>
</body>
</html>


 

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Drawing;
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;

public partial class UpLoad : System.Web.UI.Page
{
    static public ArrayList hif = new ArrayList(); // 保存文件列表
    public int filesUploaded = 0; // 上传文件的数量

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 将要上传的文件添加到listbox中
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void AddFile_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack == true)
        {
            hif.Add(HIF);
            FileList.Items.Add(HIF.PostedFile.FileName);
        }
        else
        { }

    }

    /// <summary>
    /// 从listbox中删除指定的文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void RemvFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)
        {
            TipInfo.Text = "错误 - 必须指定要删除的文件.";
            return;
        }
        else if (FileList.Items.Count != 0)
        {
            hif.RemoveAt(FileList.SelectedIndex);
            FileList.Items.Remove(FileList.SelectedItem.Text);
            TipInfo.Text = "";
        }

    }

    /// <summary>
    /// 循环上传listbox中的文件到指定的文件夹下
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Uploads_ServerClick(object sender, EventArgs e)
    {
        string baseLocation = Server.MapPath("Uploads/"); // 上传路径  
        string status = "";  // 上传成功后显示的文件列表        

        if ((FileList.Items.Count == 0) && (filesUploaded == 0))
        {
            TipInfo.Text = "错误 - 必须指定要上传的文件.";
            return;
        }
        else
        {
            foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
            {
                try
                {
                    string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
                    HIF.PostedFile.SaveAs(baseLocation + fn);
                    filesUploaded++;
                    status += fn + "<br>";
                }
                catch (Exception err)
                {
                    TipInfo.Text = "上传错误 " + baseLocation
                     + "<br>" + err.ToString();
                }
            }

            if (filesUploaded == hif.Count)
            {
                TipInfo.Text = "共上传了 " + filesUploaded + " 个文件。 <br>" + status;
            }
            hif.Clear();
            FileList.Items.Clear();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值