views:

366

answers:

1

I am using a Winforms webbrowser control to display HTML content stored in my SQL DB table. However, some of my contents are PDFs which I intend to store as binary data in the SQL DB and feed to the Webbrowser control. I wish to avoid the hassle of storing the binary content in a temporary file on the client machine and then create html container code to reference this temporary PDF file. Is there a way inwhich I can embed the PDF binary content into the html directly and show this html on the webbrowser control directly?

I found these previous threads referring to a COM 'hack'. Is there a simpler, easier way? Thanks! http://stackoverflow.com/questions/290035/how-do-i-get-a-c-webbrowser-control-to-show-jpeg-files-raw

Thanks, uniball

+1  A: 

I wouldn't say implementing an asynchronus pluggable protocol is a hack. It is the standard approach on Windows to implement a new protocol that IE will be able to use. The registering of the protocol handler does require fiddly setup though.

But the simplest/easiest way is to actually use a temporary file, and simply link to it from your html. It's not a hassle providing you take care to use the relevant .NET functions to ensure your code doesn't make assumptions about folder/file paths etc.

  1. Use GetTempPath() to get the temp folder.
  2. Create a pdf file in the temp folder with a unique name using Guid.NewGuid()
  3. Write your binary PDF data to the file.
  4. Update the html elements href or src attribute to point the full path of the temp file.
  5. Load the html into the WebBrowser control.
  6. Delete the temporary pdf file when no longer needed.

Another possible approach would be to Base64 encode your binary data and store it inside a DIV/Input element. Next add an ActiveX control or some other client side broswer component to your html page that knows how to look for the Base64 data and can turn it back into a binary stream (and display as a PDF). I'm not aware of a 3d party control that does this however.

Ash
Thanks for the steps. I think that is the way to go. I will keep this thread open to see if there are any other solutions. Thanks!