将多个BLOB下载为ZIP文件

时间:2018-09-26 07:21:50

标签: c# asp.net blob zipfile

我有一个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();
}

1 个答案:

答案 0 :(得分:0)

您需要使用其他库进行压缩。 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();
            }
        }
相关问题