基本上,用户应该能够点击一个链接并下载多个PDF文件。但问题是我不能在服务器或任何地方创建文件。一切都必须在内存中进行。
我已经能够创建内存流并将其作为PDF使用Response.Flush(),但如何压缩多个内存流而不创建文件呢?
以下是我的代码:
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 - 用于压缩
谢谢, 卡维塔
new byte[4096];
,文件大小可能会更多或更少。这样做不会产生问题吗? - Psychonaut007