【问题标题】:Download multiple BLOBs as ZIP file将多个 BLOB 下载为 ZIP 文件
【发布时间】:2019-03-01 21:38:06
【问题描述】:

我有一个 asp.net 网页,用户可以在其中选择网格中的多个项目。 当他们单击下载按钮时,他们应该能够根据所选网格项目的 id 将多个 pdf 作为 zip 文件下载。 PDF 以 blob 形式存储在 Oracle 数据库中。

我能够检索单个 blob 并在浏览器中将其显示为 pdf。 但是我很难弄清楚如何将多个 blob 作为 pdf 文件放入 zipfile 中,然后下载所述 zipfile。 如果可能,我想使用 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();
}

【问题讨论】:

    标签: c# asp.net blob zipfile


    【解决方案1】:

    您需要使用不同的库来压缩。 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();
                }
            }
    

    【讨论】:

    • 感谢您的建议。我可以使用您的代码下载压缩文件,但无法打开压缩文件。这可能是因为 blob 实际上是一个 pdf,但它是作为 zip 文件下载的,因此无法打开?
    • 对不起,我稍微改变了答案。你能再试一次吗?
    • 是的!这就像一个魅力。非常非常感谢!
    猜你喜欢
    • 2012-02-10
    • 2021-08-11
    • 2010-12-17
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 2011-06-27
    相关资源
    最近更新 更多