Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can I pass binary data of a file to WebBrowser control?

Can I pass binary data of a file to WebBrowser control?

Scheduled Pinned Locked Moved C#
databasehelpquestion
20 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    Not sure what your question is. To the OP, I see two possible paths: 1. feed an entire MemoryStream to WebBrowser.DocumentStream, by synthesizing a valid HTML response; 2. or provide an embedded HTML server, and use WebBrowser.Navigate(). In both cases, the data could be provided by any means available, such as a database. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read formatted code with indentation, so please use PRE tags for code snippets.


    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #11

    My post means that you can't display pdf in web browser control from binary data, loaded from a database for example.And the forum thread said that you can't set DocumentType property but you could work around that by hooking IE session.I believe it's ultimate hack.

    Life is a stage and we are all actors!

    L 2 Replies Last reply
    0
    • L Lost User

      My post means that you can't display pdf in web browser control from binary data, loaded from a database for example.And the forum thread said that you can't set DocumentType property but you could work around that by hooking IE session.I believe it's ultimate hack.

      Life is a stage and we are all actors!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #12

      I'm pretty sure it can be done, as I have said several times already. :|

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      1 Reply Last reply
      0
      • L Lost User

        My post means that you can't display pdf in web browser control from binary data, loaded from a database for example.And the forum thread said that you can't set DocumentType property but you could work around that by hooking IE session.I believe it's ultimate hack.

        Life is a stage and we are all actors!

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #13

        So I created http://www.codeproject.com/Tips/87045/WebBrowser-shows-File-less-local-Image.aspx[^] to prove it (for an image; a PDF would be similar). :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        L 1 Reply Last reply
        0
        • A Ahmad Safwat

          Hi all, I want to use WebBrowser control as a file viewer, but my files are stored in database. I can open files by WebBrowser control using WebBrowse.Navigate() method. But, I want to load my files directly from database to WebBrowser control, instead of saving them to hard disk then loading to WebBrowser control using WebBrowse.Navigate() method. Can I pass binary data of a file from database to WebBrowser control? please help...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #14

          I created http://www.codeproject.com/Tips/87045/WebBrowser-shows-File-less-local-Image.aspx[^] to show how it can be done for an image; a PDF would be similar. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          A 1 Reply Last reply
          0
          • L Luc Pattyn

            So I created http://www.codeproject.com/Tips/87045/WebBrowser-shows-File-less-local-Image.aspx[^] to prove it (for an image; a PDF would be similar). :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #15

            Luc Pattyn wrote:

            to prove it (for an image; a PDF would be similar).

            Yes if the pdf is located somewhere on the web.But if it's binary data loaded from local db there's only one way, which is closer to dirty hacks instead of normal logic.

            Life is a stage and we are all actors!

            L 1 Reply Last reply
            0
            • L Lost User

              Luc Pattyn wrote:

              to prove it (for an image; a PDF would be similar).

              Yes if the pdf is located somewhere on the web.But if it's binary data loaded from local db there's only one way, which is closer to dirty hacks instead of normal logic.

              Life is a stage and we are all actors!

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #16

              What is wrong with you? simply copy whatever binary data you have from the database to a memory stream, then feed the WebBrowser; it will consume everything it understands (HTML; XML; plain text; lots of image formats; PDF). PDF will only work well if you thaught Internet Explorer to handle PDF files (e.g. with an Adobe component); if it doesn't, neither will WebBrowser. :confused:

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              L 1 Reply Last reply
              0
              • L Luc Pattyn

                What is wrong with you? simply copy whatever binary data you have from the database to a memory stream, then feed the WebBrowser; it will consume everything it understands (HTML; XML; plain text; lots of image formats; PDF). PDF will only work well if you thaught Internet Explorer to handle PDF files (e.g. with an Adobe component); if it doesn't, neither will WebBrowser. :confused:

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #17

                Luc Pattyn wrote:

                PDF will only work well if you thaught Internet Explorer to handle PDF files

                Yes you are right only for images handing.It't NOT working with PDFs as it was already mentioned.Simply try that and see with your own eyes what is displayed-the pdf file or something else:

                byte[] binarydata = File.ReadAllBytes(@"C:\somefile.pdf");
                MemoryStream str = new MemoryStream(binarydata);
                webBrowser1.DocumentStream = str;

                The problem is that you can't set DocumentType property and IE can't handle Content-Type header automatically.You need to set it somewhere, but you are not able using DocumentStream property only.

                Life is a stage and we are all actors!

                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  I created http://www.codeproject.com/Tips/87045/WebBrowser-shows-File-less-local-Image.aspx[^] to show how it can be done for an image; a PDF would be similar. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #18

                  Hi Luc, I think you and the OP are miscommunicating. Let me see if I can help to clear things up. Say you want to display a PDF in a WebBrowser control. You'd have to 1) have some HTML that makes uses of <embed> to point to a PDF and 2) have that embed tag point to some location that is storing the PDF. Now, if the application making use of the WebBrowser is entirely local, it would be impossible to, say, serve up a PDF file when a certain URL is hit (that would solve the problem of using a stream to serve a PDF, but only if the OP has access to a web server). Now, if the user wants to do everything on the client side without any file access or remote URL access, then it seems there may be a problem displaying the PDF. The OP is claiming that images can be directly displayed by a WebBrowser control (i.e., by passing the memory stream to the WebBrowser) but that the WebBrowser is incapable of displaying a PDF, except for via an <embed> tag. If you use the EMBED approach, you have to point to somewhere, and that somewhere can't be a memory stream. By the way, the tip/trick you made doesn't work for me. I just see an "X" displayed where there should be an image. Not sure why that is. Perhaps the type of data needs to be specified somehow (usually this would be provided by the server in the HTTP header, but there is no HTTP header AFAIK when you set the WebBrowser control to use a memory stream).

                  [Forum Guidelines]

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Luc Pattyn wrote:

                    PDF will only work well if you thaught Internet Explorer to handle PDF files

                    Yes you are right only for images handing.It't NOT working with PDFs as it was already mentioned.Simply try that and see with your own eyes what is displayed-the pdf file or something else:

                    byte[] binarydata = File.ReadAllBytes(@"C:\somefile.pdf");
                    MemoryStream str = new MemoryStream(binarydata);
                    webBrowser1.DocumentStream = str;

                    The problem is that you can't set DocumentType property and IE can't handle Content-Type header automatically.You need to set it somewhere, but you are not able using DocumentStream property only.

                    Life is a stage and we are all actors!

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #19

                    Yes, you are right, PDF is different after all. Some research indicates PDF is harder than images, but doable. I'll try to demonstrate when time allows me. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    1 Reply Last reply
                    0
                    • A AspDotNetDev

                      Hi Luc, I think you and the OP are miscommunicating. Let me see if I can help to clear things up. Say you want to display a PDF in a WebBrowser control. You'd have to 1) have some HTML that makes uses of <embed> to point to a PDF and 2) have that embed tag point to some location that is storing the PDF. Now, if the application making use of the WebBrowser is entirely local, it would be impossible to, say, serve up a PDF file when a certain URL is hit (that would solve the problem of using a stream to serve a PDF, but only if the OP has access to a web server). Now, if the user wants to do everything on the client side without any file access or remote URL access, then it seems there may be a problem displaying the PDF. The OP is claiming that images can be directly displayed by a WebBrowser control (i.e., by passing the memory stream to the WebBrowser) but that the WebBrowser is incapable of displaying a PDF, except for via an <embed> tag. If you use the EMBED approach, you have to point to somewhere, and that somewhere can't be a memory stream. By the way, the tip/trick you made doesn't work for me. I just see an "X" displayed where there should be an image. Not sure why that is. Perhaps the type of data needs to be specified somehow (usually this would be provided by the server in the HTTP header, but there is no HTTP header AFAIK when you set the WebBrowser control to use a memory stream).

                      [Forum Guidelines]

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #20

                      Thanks for your effort, however it did not change my view on things, so maybe I misunderstood both of you now, or, well, I don't know. I'll try to summarize the main facts so far: 1. I understood the OP wants to see local data (images, PDF, whatever) which is stored in a database rather than in individual files, and wants to avoid the use of temporary files. I take it all he wants to see is one data object at a time, not a composition of some HTML and one or more database data objects. I am assuming all data types are popular types known to web browsers. If not, special viewers would be required (e.g. a HEX viewer for unstructured, "meaningless", binary data. 2. The Tip&Trick works fine on my system, which has Vista-32, IE7 as original browser, FF3 as default browser, and using a .NET 2.0 debug build. So I would be inclined to say images cause no fundamental problem, provided they use a format WebBrowser recognizes AND understands (such as GIF). 3. I have no idea yet what could go wrong for you there. I tested the exact code as published inside a pre-existing WinForm application. So I have some questions for you: (A) did you change the code at all? (B) what are your system's characteristics? (C) Knowing the WebBrowser's size is 500*500, how large is the "X"? I assume it is red? 4. If the OP uses my T&T code and replaces my synthetic image by some data read from his database, images should work. 5. Hristo Bojilov signaled PDF would not work that way, and indeed PDF seems more complicated; IE and FF successfully load and display a remote PDF (when typing an URL in the address bar), WebBrowser also successfully navigates to the same PDF file, but fails to recognize a PDF when simply feeding it in its DocumentStream. It then simply shows the bytes as text. Some research has led to two complex ways around it, one of them is an article here on CP; the basic idea is to help WebBrowser in recognizing PDF files and selecting the right way to handle them, and so it is not based on an EMBED or OBJECT tag, as they would require a file. I haven't tested these yet, I have more urgent matters right now. 6. "but only if the OP has access to a web server" I once created an embedded web server; it basically is a class one adds to an existing project; it takes data and produces web pages (that part of course needs customization), available on localhost at some port number. Doing so enables any WinApp to make data available to a browser running on the same PC, possibly a WebBrowser within the same app. As it

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups