zoukankan      html  css  js  c++  java
  • mvc 批量下载文件

    年前要实现批量下载文件

    因为快放假一直没有心情写

    年后回来第一件事就是完成批量下载文件

    使用原来的方法一直无法实现,只能实现单条记录下载

    还是在网上搜到的方法

    自己改了改

            $(".plxz").click(function () {
                debugger;
                var ids = new Array(); $('.checkboxes').each(function () {
                    if (this.checked == true) {
                        ids.push(this.value);
                    }
                })
                debugger;
                $.ajax({
                    url: '/HOS/Apprentice/DownloadFile',
                    cache: false,
                    async: true,
                    type: 'post',
                    contentType: 'application/json',
                    data: JSON.stringify(ids),
                    success: function (data) {
                        alert("下载完成");
                    }
                });
            })

    上面的代码为点击批量下载按钮时的ajax请求

      [HttpPost]
            public FileResult DownloadFile(List<string> list)
            {
                //获取服务器中的文件路径
                string filePath = Server.MapPath("~/Files/masterpapers/2017-1/");
    
                //要压缩的文件夹,把需要打包的文件存放在此文件夹
                string dPath = @"E:文件";
                //压缩后的文件存放路径
                string destFile = @"E:文件Download";
                if (list.Count > 1)
                {
                    foreach (string fileName in list)
                    {
                        string sourceFileName = Path.Combine(filePath, fileName);
                        string destFileName = Path.Combine(dPath, fileName);
                        //true为覆盖同名文件
                        System.IO.File.Copy(sourceFileName, destFileName, true);
                    }
                    //FileDown fileDown = new FileDown();
                    //返回压缩后的文件提供下载
                    ZipFileFromDirectory(dPath, destFile, 9);
                    //参数为文件存放路径,下载的文件格式,文件名字
                    return File(destFile, "application/octet-stream", Path.GetFileName(destFile));
                }
                else
                {
                    //单个文件下载,不需要打包
                    foreach (string fileName in list)
                    {
                        string sourceFileName = Path.Combine(filePath, fileName);
                        string destFileName = Path.Combine(dPath, fileName) + ".xls";
                        System.IO.File.Copy(sourceFileName, destFileName, true);
                    }
                    //参数为文件存放路径,下载的文件格式,文件名字
                    return File(destFile, "application/csv", Path.GetFileName(destFile));
                }
            }

    上面的方法为控制器中DownloadFile方法

    ZipFileFromDirectory 这个方法主要是将下载文件进行压缩,用不到的可以忽略
     public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)
            {
                GetAllDirectories(rootPath);
                //得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。  
                string rootMark = rootPath + "\";
                //Crc32校验
                Crc32 crc = new Crc32();
                //zip输出类
                ZipOutputStream outPutStream = new ZipOutputStream(System.IO.File.Create(destinationPath));
                // 0-9程序的压缩,设置压缩程度
                outPutStream.SetLevel(compressLevel);
                //将文件读入压缩流
                foreach (string file in files)
                {
                    FileStream fileStream = System.IO.File.OpenRead(file);
                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    //设置文件的一些参数
                    ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
                    entry.DateTime = DateTime.Now;
                    entry.Size = fileStream.Length;
                    fileStream.Close();
                    //计算Crc32检验码
                    crc.Reset();
                    crc.Update(buffer);
                    //设置校验码
                    entry.Crc = crc.Value;
                    //将当前文件的zip文件流写入输出流
                    outPutStream.PutNextEntry(entry);
                    outPutStream.Write(buffer, 0, buffer.Length);
                }
                this.files.Clear();
                foreach (string emptyPath in paths)
                {
                    ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
                    outPutStream.PutNextEntry(entry);
                }
                this.paths.Clear();
                outPutStream.Finish();
                outPutStream.Close();
                GC.Collect();
            }
    取得目录下所有文件和文件夹
        /// <summary>  
            /// 取得目录下所有文件及文件夹,分别存入files及paths  
            /// </summary>  
            /// <param name="rootPath">根目录</param>  
            private void GetAllDirectories(string rootPath)
            {
                //获取所有的子目录  
                string[] subPaths = Directory.GetDirectories(rootPath);
                foreach (string path in subPaths)
                {
                    //找到子目录并将当前目录的文件名存入List  
                    GetAllDirectories(path);
                }
                string[] files = Directory.GetFiles(rootPath);
                foreach (string file in files)
                {
                    //将当前目录中的所有文件全名存入文件list  
                    this.files.Add(file);
                }
                //判断是否是空目录  
                if (subPaths.Length == files.Length && files.Length == 0)
                {
                    //如果是空目录则记录空目录 
                    this.paths.Add(rootPath);
                }
            }



  • 相关阅读:
    乔治·奥威尔的六条有效写作的规则
    读书:《个人形成论》 Carl R. Rogers
    想想体制性的生存法则
    每一个山峰都建立在同一座山上
    读书笔记:这些道理没有人告诉过你(二)
    举国的不仅仅是运动员
    参加了一个社会化营销策划比赛整理一下参考资料
    读书:《个人形成论》2 Carl R. Rogers
    前端避坑指南丨辛辛苦苦开发的 APP 竟然被判定为简单网页打包?
    Entify Framework 4.1[问题集] 一个实体的双向依赖引起的错误
  • 原文地址:https://www.cnblogs.com/jiangyou-lz/p/6371192.html
Copyright © 2011-2022 走看看