C#中⽂件多选批量下载
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
/// <summary>
/// 批量进⾏多个⽂件压缩到⼀个⽂件、
/// <param name="files">⽂件列表(绝对路径)</param> 这⾥⽤的数组,你可以⽤list 等或者
/// <param name="zipFileName">⽣成的zip⽂件名称</param>
/// </summary>
public void ZipFileDownload(string[] files, string zipFileName,HttpContext Context)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom();
foreach (var item in files)
{
if (File.Exists(Context.Server.MapPath(item)))
file.Add(Context.Server.MapPath(item));
}
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length); //读取⽂件内容(1次读ms.Length/1024M)
ms.Flush();
ms.Close();
}
Context.Response.Clear();
param nameContext.Response.Buffer = true;
Context.Response.ContentType = "application/x-zip-compressed";
Context.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName)); Context.Response.BinaryWrite(buffer);
Context.Response.Flush();
Context.Response.End();
}
}
//调⽤也可以⽤集合
//string[] aa = { "/DownloadBuyPic/20161019073315220.zip", "/DownloadBuyPic/BigPic/20161020043805152.jpg" }; //mt.ZipFileDownload(aa, DateTime.Now.ToString("yyyyMMddhhMmss") + "_检品报告书.zip",HttpContext.Current);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论