【发布时间】:2012-04-18 05:21:56
【问题描述】:
基本上,用户应该能够单击一个链接并下载多个 pdf 文件。但问题是我无法在服务器或任何地方创建文件。一切都必须在记忆中。
我能够创建内存流和 Response.Flush() 它作为 pdf,但是如何在不创建文件的情况下压缩多个内存流。
这是我的代码:
Response.ContentType = "application/zip";
// If the browser is receiving a mangled zipfile, IIS Compression may cause this problem. Some members have found that
// Response.ContentType = "application/octet-stream" has solved this. May be specific to Internet Explorer.
Response.AppendHeader("content-disposition", "attachment; filename=\"Download.zip\"");
Response.CacheControl = "Private";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); // or put a timestamp in the filename in the content-disposition
byte[] abyBuffer = new byte[4096];
ZipOutputStream outStream = new ZipOutputStream(Response.OutputStream);
outStream.SetLevel(3);
#region Repeat for each Memory Stream
MemoryStream fStream = CreateClassroomRoster();// This returns a memory stream with pdf document
ZipEntry objZipEntry = new ZipEntry(ZipEntry.CleanName("ClassroomRoster.pdf"));
objZipEntry.DateTime = DateTime.Now;
objZipEntry.Size = fStream.Length;
outStream.PutNextEntry(objZipEntry);
int count = fStream.Read(abyBuffer, 0, abyBuffer.Length);
while (count > 0)
{
outStream.Write(abyBuffer, 0, count);
count = fStream.Read(abyBuffer, 0, abyBuffer.Length);
if (!Response.IsClientConnected)
break;
Response.Flush();
}
fStream.Close();
#endregion
outStream.Finish();
outStream.Close();
Response.Flush();
Response.End();
这会创建一个 zip 文件,但里面没有文件
我正在使用 使用 iTextSharp.text - 用于创建 pdf 使用 ICSharpCode.SharpZipLib.Zip - 用于压缩
谢谢, 卡维塔
【问题讨论】:
-
您找到解决方案了吗?我需要做同样的事情。
-
不,我没有......我在网上尝试了所有解决方案,但没有任何效果......希望你能找到它
-
此链接可能会有所帮助:snipplr.com/view/47762
标签: c# zip memorystream