zoukankan      html  css  js  c++  java
  • asp.net 多个文件同时下载

    需求:

    支持文件批量下载。现在有很多小图片需要批量下载,不希望在服务器打包下载。

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

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

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

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

    支持从URL中下载文件。

    支持JSON数据结构。

     

    方案:

    1、首先读取文件夹下的文件,可能同时存在多个文件

    2、选中文件,然后点击下载,同时可以选择多个文件。

    思路:通过生产压缩包的形式进行下载,然后再清楚压缩包,这样用户可以一次性全部下载下来。

    一、获取目录下的所有文件,然后绑定到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();

            }

        }

     

    三、系统中需要引用的dll http://blog.ncmem.com/wordpress/2019/08/28/java批量下载/ 需要下载。

    四、运行效果如图:

     

  • 相关阅读:
    HDOJ 1202 The calculation of GPA
    HDOJ 1197 Specialized Four-Digit Numbers
    HDOJ 1196 Lowest Bit(二进制相关的简单题)
    HDOJ 1194 Beat the Spread!(简单题)
    NOIP2018游记
    CF1043
    洛谷P1280 尼克的任务
    洛谷P1155 双栈排序
    SPOJ6340 ZUMA
    chessboard
  • 原文地址:https://www.cnblogs.com/xproer/p/11542883.html
Copyright © 2011-2022 走看看