首页 > 代码库 > Asp.net 获取图片列表并打包下载
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 }
Asp.net 获取图片列表并打包下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。