简体   繁体   中英

Download multiple BLOBs as ZIP file

I have an asp.net webpage where users can select multiple items in a grid. 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. The PDFs are stored in an Oracle database as blobs.

I am able to retrieve a single blob and display it as a pdf in the browser. 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. I would like to make use of the System.IO.Compression library if possible.

Here's what my code looks like now, to display a single 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

  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();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM