Response.BinaryWrite
-
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
-
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
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(). -
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().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.
-
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
-
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.
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.
-
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.
-
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
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