多文件打包下载(SharpZipLib)

原理:1.将需要下载的文件保存到服务器上 (引用using ICSharpCode.SharpZipLib.Zip;)

EasyUIGridModel result = bll.Query(gridSetting.PageIndex, gridSetting.PageSize, AutoMapping.ConvertDto2Ht<BuyProView>(criteria), WorkContext.Identity, gridSetting.SortBy);
            foreach (var date in (List<dynamic>)result.Rows)
            {
//数据已二进制流保存在数据库中
                if (date.BILLFILE != null)
                {
                    string savepath = Server.MapPath("~/") + string.Format("DownloadPdf/发票{0}_{1}.pdf", date.NO, DateTime.Now.ToString("yyyyMMdd"));  //文件位置

                    FileStream fs = new FileStream(savepath, FileMode.CreateNew);//将二进制写入内存保存
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(date.BILLFILE, 0, date.BILLFILE.Length);  //保存文件
                    bw.Close();
                    fs.Close();
                }

            }

将文件打包到指定位置(服务器)

 var filenames = Directory.GetFiles(Server.MapPath("~/DownloadPdf"));  //需压缩的文件地址
            var pdfpath =Server.MapPath("~/")+ string.Format("文档_{0}.zip", DateTime.Now.ToString("yyyyMMdd"));//打包文件地址
            using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Open(pdfpath, FileMode.OpenOrCreate)))
            {

                s.SetLevel(6); // 压缩级别 0-9
                foreach (string file in filenames)
                {
                    if (System.IO.File.Exists(file))
                    {
                        FileInfo item = new FileInfo(file);
                        FileStream fs = System.IO.File.OpenRead(item.FullName);   //读取文件流
                        byte[] buffer = new byte[fs.Length];   
                        fs.Read(buffer, 0, buffer.Length);   //将文件读成2进制流

                        ZipEntry entry = new ZipEntry(item.Name);
                        s.PutNextEntry(entry);   //将文件放入压缩流中
                        s.Write(buffer, 0, buffer.Length);  //保存
                        fs.Close();
                        System.IO.File.Delete(file);   //删除放入服务器的文件
                    }
                }

            }
            FileStream fszip = new FileStream(pdfpath, FileMode.Open);   //读取压缩文件
            byte[] bytes = new byte[(int)fszip.Length];  //将压缩文件保存为2进制流
            fszip.Read(bytes, 0, bytes.Length);
            fszip.Close();
            System.IO.File.Delete(pdfpath);   //删除保存在服务器上的压缩文件

下载文件 (Response下载到客户端)
Response.Charset = “UTF-8”;
Response.ContentEncoding = System.Text.Encoding.GetEncoding(“UTF-8”);
Response.ContentType = “application/octet-stream”;
Response.AddHeader(“Content-Disposition”, “attachment; filename=” + Server.UrlEncode(string.Format(“文档_{0}.zip”, DateTime.Now.ToString(“yyyyMMdd”)))); //以流媒体形式下载
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值