[英]Download multiple BLOBs as ZIP file
I have an asp.net webpage where users can select multiple items in a grid. 我有一个asp.net网页,用户可以在其中选择网格中的多个项目。 When they click a download button, they should be able to download multiple pdf's as a zipfile based on the id's of the selected grid items. 当他们单击下载按钮时,他们应该能够基于所选网格项的ID将多个pdf作为zip文件下载。 The PDFs are stored in an Oracle database as blobs. PDF以blob的形式存储在Oracle数据库中。
I am able to retrieve a single blob and display it as a pdf in the browser. 我能够检索单个blob并将其显示为pdf在浏览器中。 But I'm having a hard time figuring out how to put multiple blobs as pdf's in a zipfile and then downloading said zipfile. 但是我很难弄清楚如何将多个blob作为pdf放在zip文件中,然后再下载该zip文件。 I would like to make use of the System.IO.Compression library if possible. 如果可能的话,我想使用System.IO.Compression库。
Here's what my code looks like now, to display a single pdf: 这是我的代码现在的样子,只显示一个pdf:
OracleBlob oBlob = null;
byte[] bBlob = null;
using (OracleDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
sFileId = reader.GetInt32(0).ToString();
oBlob = reader.GetOracleBlob(1);
if (!oBlob.IsNull)
{
bBlob = new byte[oBlob.Length];
oBlob.Read(bBlob, 0, (int)oBlob.Length);
}
}
}
if (bBlob != null)
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;");
HttpContext.Current.Response.AddHeader("content-length", bBlob.Length.ToString());
HttpContext.Current.Response.BinaryWrite(bBlob);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
you need to use a different library to zip. 您需要使用其他库进行压缩。 DotNetZip DotNetZip
using (var zip = new ZipFile())
{
zip.AddEntry("zpn.pdf", bBlob);
using (var memoryStream = new MemoryStream())
{
zip.Save(memoryStream);
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("content-disposition", " inline; filename=\"myfile.zip");
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.End();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.