[英]Download multiple BLOBs as ZIP file
我有一個asp.net網頁,用戶可以在其中選擇網格中的多個項目。 當他們單擊下載按鈕時,他們應該能夠基於所選網格項的ID將多個pdf作為zip文件下載。 PDF以blob的形式存儲在Oracle數據庫中。
我能夠檢索單個blob並將其顯示為pdf在瀏覽器中。 但是我很難弄清楚如何將多個blob作為pdf放在zip文件中,然后再下載該zip文件。 如果可能的話,我想使用System.IO.Compression庫。
這是我的代碼現在的樣子,只顯示一個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();
}
您需要使用其他庫進行壓縮。 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.