views:

494

answers:

3

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.

+1  A: 

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

great_llama
+2  A: 

Hello again.

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.

Jason Heine
okay i am going to try this out
this one worked, but I am looking at the other responses too
+3  A: 
        Response.ContentType = "application/pdf";
        byte[] bytes = YourBinaryContent;

        using (BinaryWriter writer = new BinaryWriter(context.Response.OutputStream))
        {
            writer.Write(bytes, 0, bytes.Length);
        }
DaDa
okay i am going to try this out
This one did not work for me... context is throwing an error, is it a typo?
context is a HttpContext passing to the Handler that shows your PDF file. You can remove it anyway
DaDa