• Asp.net 获取图片列表并打包下载


    先引用:ICSharpCode.SharpZipLib.dll

    后台代码:

    using System.IO;
    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.Checksums;
    using ICSharpCode.SharpZipLib.GZip;
    
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            DataTable dt1 = OleDbHelper.GetDataTable("select tupian from cp_img where cpid=" + ltl_id.Text + " and lx=0 order by px asc");
            DataTable dt2 = OleDbHelper.GetDataTable("select tupian from cp_img where cpid=" + ltl_id.Text + " and lx=1 order by px asc");
            if (dt1.Rows.Count > 0)
            {
                MemoryStream ms = new MemoryStream();
                byte[] buffer = null;
    
                using (ZipFile file = ZipFile.Create(ms))
                {
                    file.BeginUpdate();
                    file.NameTransform = new MyNameTransfom();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
    
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        file.Add(Server.MapPath(dt1.Rows[i]["tupian"].ToString()));
    
                        for (int j = 0; j < dt2.Rows.Count; j++)
                        {
                            file.Add(Server.MapPath(dt2.Rows[j]["tupian"].ToString()));
                        }
                    }
    
                    file.CommitUpdate();
    
                    buffer = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(buffer, 0, buffer.Length);
                }
    
                string filename = ltl_mc.Text + "-" + DateTime.Now.ToString() + ".zip";
    
                Response.AddHeader("content-disposition", "attachment;filename=" + filename + "");
                Response.BinaryWrite(buffer);
                Response.Flush();
                Response.End();
            }
        }
    
        public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
        {
    
            #region INameTransform 成员
    
            public string TransformDirectory(string name)
            {
                return null;
            }
    
            public string TransformFile(string name)
            {
                return Path.GetFileName(name);
            }
    
            #endregion
        }
    

      

  • 相关阅读:
    I/O多路复用
    Django重点之url别名
    10 个常用的 Linux 命令?
    软连接和硬链接的区别?
    Linux 重定向命令有哪些?有什么区别?
    在 linux 中 find 和 grep 的区别??
    Django中用 form 实现登录注册
    你所遵循的PEP8代码规范是什么?请举例说明其要求?
    什么是 Python 的命名空间?
    ELK+Kafka日志收集环境搭建
  • 原文地址:https://www.cnblogs.com/liliang2015/p/4264339.html
Copyright © 2020-2023  润新知