asp.net 多個文件同時下載

需求:java

支持文件批量下載。如今有不少小圖片須要批量下載,不但願在服務器打包下載。chrome

支持大文件斷點下載。好比下載10G的文件。json

PC端全平臺支持。Windows,macOS,Linux瀏覽器

全瀏覽器支持。ie6,ie7,ie8,ie9,ie10,ie11,edge,firefox,chrome,safari服務器

支持文件夾結構下載。不但願在服務器打包,而是直接下載文件夾,下載後在本地文件夾結構和服務器保持一致。數據結構

支持從URL中下載文件。app

支持JSON數據結構。wordpress

 

方案:url

1、首先讀取文件夾下的文件,可能同時存在多個文件spa

2、選中文件,而後點擊下載,同時能夠選擇多個文件。

思路:經過生產壓縮包的形式進行下載,而後再清楚壓縮包,這樣用戶能夠一次性所有下載下來。

1、獲取目錄下的全部文件,而後綁定到checkboxlist中 ,代碼以下:

 

批量下載多個文件

$("#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);

});

 

自定義JSON數據下載

$("#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);

});

 

服務端代碼:

 protected void Btn_down_Click(object sender, EventArgs e)

    {

        if (ckl_ck.Items.Count > 0)

        {

            List<string> listFJ = new List<string>();//保存附件路徑

            List<string> listFJName = new List<string>();//保存附件名字

            for (int i = 0; i < ckl_ck.Items.Count; i++)

            {

                if (ckl_ck.Items[i].Selected)

                {

                    listFJ.Add(Server.MapPath("Resource/Help/") + ckl_ck.Items[i].Text);

                    listFJName.Add(ckl_ck.Items[i].Text);

                }

 

            }

            string time = DateTime.Now.Ticks.ToString();

            ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("Resource/Help/" + time + ".zip"), 9);//壓縮文件

            DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("Resource/Help/" + time + ".zip"));//下載文件        }

    }

    private void DownloadFile(string fileName, string filePath)

    {

        FileInfo fileInfo = new FileInfo(filePath);

        Response.Clear();

        Response.ClearContent();

        Response.ClearHeaders();

        Response.AddHeader("Content-Disposition""attachment;filename=" + fileName);

        Response.AddHeader("Content-Length", fileInfo.Length.ToString());

        Response.AddHeader("Content-Transfer-Encoding""binary");

        Response.ContentType = "application/octet-stream";

        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

        Response.WriteFile(fileInfo.FullName);

        Response.Flush();

        File.Delete(filePath);//刪除已下載文件        Response.End();

    }

 

    /// <summary>

    ///壓縮文件

    ///</summary>

    /// <param name="fileName">要壓縮的全部文件(徹底路徑)</param>

    /// <param name="fileName">文件名稱</param>

    /// <param name="name">壓縮後文件路徑</param>

    /// <param name="Level">壓縮級別</param>

    public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)

    {

        ZipOutputStream s = new ZipOutputStream(File.Create(name));

        Crc32 crc = new Crc32();

        //壓縮級別

        s.SetLevel(Level); // 0 - store only to 9 - means best compression

        try

        {

            int m = 0;

            foreach (string file in filenames)

            {

                //打開壓縮文件

                FileStream fs = File.OpenRead(file);//文件地址

                byte[] buffer = new byte[fs.Length];

                fs.Read(buffer, 0, buffer.Length);

                //創建壓縮實體

                ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名

                //時間

                entry.DateTime = DateTime.Now;

                //空間大小

                entry.Size = fs.Length;

                fs.Close();

                crc.Reset();

                crc.Update(buffer);

                entry.Crc = crc.Value;

                s.PutNextEntry(entry);

                s.Write(buffer, 0, buffer.Length);

                m++;

            }

        }

        catch

        {

            throw;

        }

        finally

        {

            s.Finish();

            s.Close();

        }

    }

 

3、系統中須要引用的dll http://blog.ncmem.com/wordpress/2019/08/28/java批量下載/ 須要下載。

4、運行效果如圖:

 

相關文章
相關標籤/搜索