4

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.

5
  • Okay, after looking at the answers 2 are correct. Which do i choose as the right answer? I am new to this and don't want to offend anyone... Commented May 13, 2009 at 16:35
  • 2
    It's your choice really. But I would pick the one that is most effective for you and the code that is the most efficient. Commented May 13, 2009 at 16:39
  • Okay, I did my research and found that the using will dispose of the object properly. I like how Jason's answer is just simple and easy to read, but yet I like how your answer (DaDa) is calling the methods a little better. It was a tough choice, but DaDa wins, sorry Jason Commented May 13, 2009 at 17:33
  • 1
    :) you made the right choice, his code is cleaner and handles the object better then my example. Thanks for thinking of me :D Commented May 13, 2009 at 17:34
  • you are so friendly and communicating people! Welcome at SOverflow Computer Girl (as you were new ). Regards also to Jason! (sorry for my English) Commented May 13, 2009 at 19:02

3 Answers 3

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

        using (BinaryWriter writer = new BinaryWriter(context.Response.OutputStream))
        {
            writer.Write(bytes, 0, bytes.Length);
        }
Sign up to request clarification or add additional context in comments.

2 Comments

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
2

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.