首页 > Web开发 > 详细

MVC 图片添加到压缩包导出

时间:2019-03-01 11:17:49      阅读:185      评论:0      收藏:0      [点我收藏+]

1、引用ICSharpCode.SharpZipLib 这个DLL 、(QRCoder我这里导出的是二维码图片,我多加了一个dll,这些DLL都可以在NuGet包里面下载的)

技术分享图片
 1  /// <summary>
 2         /// 压缩打包文件 导出
 3         /// </summary>
 4         public ActionResult ZipFileByCode(string Door_Autoid)
 5         {
 6             //条件不满足时返回的信息
 7             if (条件)
 8             {
 9                 Response.Write("<script>alert(‘返回提示信息!‘);opener=null;self.close();</script>");
10                 Response.End();
11                 return new EmptyResult();
12             }
13             //此处是拿到数据,不用管,每个人获取数据方式都不一样
14             List<DoorInfo> Doors = 获取数据;
15             List<string> Autoids = Door_Autoid.Split(,).ToList();
16            
17             //保存导出路径  添加压缩包的时候
18             List<string> savePaths = new List<string>();
19             //保存二维码图片
20             foreach (DoorInfo item in Doors)
21             {
22 
23                 // 生成二维码的内容
24                 string strCode = item.URL; 
25                 QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
26                 QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
27                 QRCode qrcode = new QRCode(qrCodeData);
28 
29                 // qrcode.GetGraphic 方法可参考最下发“补充说明”
30                 Bitmap qrCodeImage = qrcode.GetGraphic(10, Color.Black, Color.White, null, 15, 6, false);
31                 //Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.White, Color.Black, null, 15, 6, false);
32                 string tempname = item.Door_Code + "_" + item.Door_Name;
33                 string filname = tempname.Length > 100 ? tempname.Substring(0, 100) + "..." : tempname;
34                 string path = AppDomain.CurrentDomain.BaseDirectory;
35                 string fileFullPath = AppDomain.CurrentDomain.BaseDirectory + "Content\\TempImages\\" + filname + ".png";
36                 //创建文件夹
37                 string temppath = path + "Content\\TempImages\\";
38                 if (!Directory.Exists(temppath))
39                     Directory.CreateDirectory(temppath);
40 
41                 if (System.IO.File.Exists(fileFullPath))
42                 {
43                     // 3.2、删除文件
44                     System.IO.File.Delete(fileFullPath);
45                 }
46                 qrCodeImage.Save(fileFullPath, ImageFormat.Png);
47 
48                 savePaths.Add("/Content/TempImages/" + filname + ".png");
49             }
50 
51             //文件名乱码解决
52             Encoding gbk = Encoding.GetEncoding("gbk");
53             ZipConstants.DefaultCodePage = gbk.CodePage;
54             //添加压缩 导出二维码
55             MemoryStream ms = new MemoryStream();
56             byte[] buffer = null;
57 
58             using (ZipFile file = ZipFile.Create(ms))
59             {
60                 file.BeginUpdate();
61 //这个我写的当前的Controller名
62                 file.NameTransform = new DoorInfoController();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
63 
64                 foreach (var item in savePaths)
65                 {
66                     file.Add(Server.MapPath(item));
67                 }
68 
69                 file.CommitUpdate();
70 
71                 buffer = new byte[ms.Length];
72                 ms.Position = 0;
73                 ms.Read(buffer, 0, buffer.Length);
74             }
75 //这里可以改动导出压缩包的名称filename
76             Response.AddHeader("content-disposition", "attachment;filename=QRCodeImages.zip");
77             Response.BinaryWrite(buffer);
78             Response.Flush();
79             Response.End();
80 
81             return new EmptyResult();
82         }
View Code

注:如果是只想导出图片的话,只需要自己拿到List<图片地址>的集合,然后传到savePaths导出路径集合,用下面的代码就行了

MVC 图片添加到压缩包导出

原文:https://www.cnblogs.com/MycnBlogs7854/p/10455166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!