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. Other Discussions
  3. Clever Code
  4. Response.BinaryWrite

Response.BinaryWrite

Scheduled Pinned Locked Moved Clever Code
question
7 Posts 4 Posters 5 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.
  • M Offline
    M Offline
    maxJared
    wrote on last edited by
    #1

    This code doesn't work. It displays a blank page. protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } but this does work when i press the button to cause a postback: protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } ?????? J

    T E I 3 Replies Last reply
    0
    • M maxJared

      This code doesn't work. It displays a blank page. protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } but this does work when i press the button to cause a postback: protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } ?????? J

      T Offline
      T Offline
      Tom Welch
      wrote on last edited by
      #2

      By closing the Response in the Page_Load you are aborting the execution flow that ASP.NET performs to return the content to the client. From the MSDN Response.Close() Closes the socket connection to a client. So you are just hanging up on the client. Response.End() Sends all currently buffered output to the client, stops execution of the page, and raises the Application_EndRequest event. So you are ending the call prematurely. If you override the OnPreRender event then ASP.NET has done enough to get the response the to client and Response.Close() will be just fine as the message was sent already... just like in your Button_Click. But you probably (read "really") want to Response.End().

      E 1 Reply Last reply
      0
      • T Tom Welch

        By closing the Response in the Page_Load you are aborting the execution flow that ASP.NET performs to return the content to the client. From the MSDN Response.Close() Closes the socket connection to a client. So you are just hanging up on the client. Response.End() Sends all currently buffered output to the client, stops execution of the page, and raises the Application_EndRequest event. So you are ending the call prematurely. If you override the OnPreRender event then ASP.NET has done enough to get the response the to client and Response.Close() will be just fine as the message was sent already... just like in your Button_Click. But you probably (read "really") want to Response.End().

        E Offline
        E Offline
        Ed Poore
        wrote on last edited by
        #3

        Shame on you:

        Subtle Bugs is a forum to post examples of interesting, aggravating and subtle bugs that you've found and fixed. Do not post programming questions in this forum. This forum is purely for amusement and discussions and all actual programming questions will be removed.

        T 1 Reply Last reply
        0
        • M maxJared

          This code doesn't work. It displays a blank page. protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } but this does work when i press the button to cause a postback: protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } ?????? J

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          [edit]My apologies, read the post too quickly & too late in the night :-O[/edit]

          Last modified: 2hrs 2mins after originally posted --

          1 Reply Last reply
          0
          • E Ed Poore

            Shame on you:

            Subtle Bugs is a forum to post examples of interesting, aggravating and subtle bugs that you've found and fixed. Do not post programming questions in this forum. This forum is purely for amusement and discussions and all actual programming questions will be removed.

            T Offline
            T Offline
            Tom Welch
            wrote on last edited by
            #5

            Relax a bit there Ed. If you read his post he explained what he found that didn't work. And then found what did work. Maybe it should be removed because of the string of question marks that followed the message. I took it to mean "WTF?" rather than "please do my homework for me". If anything this is a case of "not a bug even though you thought it was". I guess I should have put that in my original reply.

            E 1 Reply Last reply
            0
            • T Tom Welch

              Relax a bit there Ed. If you read his post he explained what he found that didn't work. And then found what did work. Maybe it should be removed because of the string of question marks that followed the message. I took it to mean "WTF?" rather than "please do my homework for me". If anything this is a case of "not a bug even though you thought it was". I guess I should have put that in my original reply.

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #6

              Ah, oops, a bit late in the night and a bit too quick at reading it :-O

              1 Reply Last reply
              0
              • M maxJared

                This code doesn't work. It displays a blank page. protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } but this does work when i press the button to cause a postback: protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.ContentType = "Application/pdf"; byte [] myBytes = somePDFFileInBinaryFormat; Response.Buffer = true; Response.BinaryWrite(myBytes); Response.Flush(); Response.Close(); } ?????? J

                I Offline
                I Offline
                icestatue
                wrote on last edited by
                #7

                Try to put the Byte array into a MemoryStream first. I had the same problem. I also found that Adobe is a crap product in general. Try checking to see if a message box appears behind your Web Application. Sometimes the application hangs up waiting for you to address a message box hiden behind your app. I might be able to dig up my original sample but now it is late for me I must go.

                nothing

                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