Question

Okay,

I have a SQL 2005 database with a stored PDF. The column uses the image type. So when the PDF gets stores it is a byte[] array.

So here is the hard part. I can get it to store the data but I am not sure how to go about getting the byte[] array back into PDF format and display it on the web page.

I am using a 3rd part tool to generate the PDF, which works great.

The code I have to generate the byte[] array is as follows:

Doc document = new Doc();
byte[] myArray = document.GetData();

I then insert the byte[] array into the database.

When I get the array back from the database I do this:

public byte[] GetPDF(parameters)
{
    return DAL.GetPDF(parameters)
}

This returns a valid byte[] array as it is supposed to.

Now, the only issue is actually writing the PDF to the web site and having it display on the web site, I am not sure how to do this or where to even begin.

Was it helpful?

Solution

        Response.ContentType = "application/pdf";
        byte[] bytes = YourBinaryContent;

        using (BinaryWriter writer = new BinaryWriter(context.Response.OutputStream))
        {
            writer.Write(bytes, 0, bytes.Length);
        }

OTHER TIPS

This is quite simple. In fact I just did this a month ago. We had to store the PDF in the database and then write it out when the customer requested to view their document.

What you need to do is on your web page add the following code:

byte[] myarray = BusinessLayer.GetPDF(parameters)

Response.Clear();
Response.ContentType = "application/pdf";
Response.BinaryWrite(myarray);

See if that works for ya.

You'll want to set the Response.ContentType to "application/pdf", and then call the Response.BinaryWrite method, passing in the byte data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top