asp.net批量下载文件

需求:

支持文件批量下载。现在有很多小图片需要批量下载。

不在服务器打包下载。因为下载的文件可能很大,比如超过5G,打包的话服务器耗时比较长。而且下载的人比较多,很容易会造成服务器磁盘空间不足。

支持大文件断点下载。比如下载10G的文件。

PC端全平台支持。Windows,macOS,Linux

全浏览器支持。ie6,ie7,ie8,ie9,ie10,ie11,edge,firefox,chrome,safari

支持文件夹结构下载。不希望在服务器打包,而是直接下载文件夹,下载后在本地文件夹结构和服务器保持一致。

支持从URL中下载文件。

支持JSON数据结构。

 

先放一段打包的代码。在小项目或局域网的一两个的项目中,这种打包下载方式没有太大的问题。但是在人数较多的项目中(比如超过100,或者是达到1000人)这种方式会占用较多的服务器资源,造成IO性能下降

Default.aspx:

<%@ Page Language="C#" 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">

        <p>

          <asp:Button ID="PackDown" runat="server" Text="打包下载" OnClick="PackDown_Click" /></p>

        <asp:GridView ID="GridView1" runat="server" Width="500px" CellPadding="8" CellSpacing="1">

          <Columns>

            <asp:TemplateField HeaderText="<input type=" checkbox "/>" InsertVisible="False">

              <ItemTemplate>

                <asp:CheckBox ID="CheckBox1" runat="server" /></ItemTemplate>

              <ItemStyle HorizontalAlign="Center" /></asp:TemplateField>

            <asp:TemplateField HeaderText="文件列表" InsertVisible="False">

              <EditItemTemplate>

                <asp:Label ID="Label1" runat="server" Text='<%# eval_r("Name") %>'></asp:Label>

              </EditItemTemplate>

              <ItemTemplate>

                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>

              </ItemTemplate>

            </asp:TemplateField>

          </Columns>

        </asp:GridView>

      </form>

    </body>

 

  </html>

 

Default.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.IO;

using Ionic.Zip;

 

public partial class _Default: System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {

        if (!Page.IsPostBack) {

            BindFilesList();

        }

    }

 

    void BindFilesList() {

        List < System.IO.FileInfo > lstFI = new List < System.IO.FileInfo > ();

        string[] files = System.IO.Directory.GetFiles("d:\\webroot");

        foreach(var s in files) {

            lstFI.Add(new System.IO.FileInfo(s));

        }

 

        GridView1.DataSource = lstFI;

        GridView1.DataBind();

    }

 

    protected void PackDown_Click(object sender, EventArgs e) {

        Response.Clear();

        Response.ContentType = "application/zip";

        Response.AddHeader("content-disposition", "filename=DotNetZip.zip");

        using(ZipFile zip = new ZipFile(System.Text.Encoding.Default)) //解决中文乱码问题

        {

            foreach(GridViewRow gvr in GridView1.Rows) {

                if (((CheckBox) gvr.Cells[0].Controls[1]).Checked) {

                    zip.AddFile("d:\\webroot\\" + (gvr.Cells[1].Controls[1] as Label).Text, ""); //AddFile()第二个参数填写时不打包路径

                }

            }

 

            zip.Save(Response.OutputStream); //输出到浏览器下载

        }

 

        Response.End();

    }

}

 

新代码

 

批量下载多个文件

$("#btn-down-files").click(function () {

    if (downer.Config["Folder"] == "") { downer.open_folder(); return; }

    var urls = [

        { fileUrl: "http://res2.ncmem.com/res/images/ie11.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/firefox.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/edge.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/home/w.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/img.png" }

    ];

    downer.app.addUrls(urls);

});

 

批量下载文件,服务器不需要打包,直接提供URL就可以批量下载。下载后用户可以直接使用,不需要解压,提高工作效率。

$("#btn-down-json").click(function () {

    if (downer.Config["Folder"] == "") { downer.open_folder(); return; }

    var fd = {

        nameLoc: "图片列表"

        , files: [

            { fileUrl: "http://res2.ncmem.com/res/images/ie11.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/firefox.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/edge.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/home/w.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/img.png" }

        ]

    };

    downer.app.addJson(fd);

});

 

实现效果:

asp.net批量下载文件

 

网上示例:http://blog.ncmem.com/wordpress/2019/08/28/net文件批量下载/