天天看點

asp.net批量下載下傳檔案 - Xproer-松鼠

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批量下載下傳檔案 - Xproer-松鼠

網上示例:http://blog.ncmem.com/wordpress/2019/08/28/net檔案批量下載下傳/

asp.net批量下載下傳檔案 - Xproer-松鼠