.NET Bug with BinaryWrite or just bad code?

G

Gregory Silvano

I have reproduced this problem on several machines (all different
configs) and don't see anything wrong with the code. The code below
(or very similar) is from Microsoft (help files, newsgroup posts,
etc.) and available at several other resources on the Web.

If you click on the LinkButton once or twice it works without
problems. However, on the third (sometimes the second) click the
browser hangs. It never even gets to a breakpoint in the PageLoad
event. Sometimes it'll just spit out the gif binary code in the page,
floating above the existing browser page like a watermark. Weird.

Can somebody reproduce this behavior?

Just create a WebApplication and throw a LinkButton on the page,
keeping the default name. Put the code below in the codebehind and
throw a gif or something in the same directory as your web form,
calling it test.gif.


Private Sub LinkButton1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles LinkButton1.Click

Dim MyFileStream As System.IO.FileStream = New _
System.IO.FileStream(Server.MapPath("test.gif"), _
System.IO.FileMode.Open)

'Get the file length
Dim FileSize As Integer = MyFileStream.Length

'Read the file into the buffer and close the file stream
Dim Buffer(CInt(FileSize)) As Byte
MyFileStream.Read(Buffer, 0, CInt(FileSize))
MyFileStream.Close()

'I've also tried Response.Clear and .ClearContents
Response.ClearHeaders()

Response.ContentType = "application/octet-stream"

'We want to force a download
Response.AddHeader("Content-Disposition", _
"attachment; filename=test123.gif")
Response.AddHeader("Content-Length", FileSize)

Response.BinaryWrite(Buffer)

End Sub


I appreciate any insight you may have. I have been having problems
getting WriteFile to work in SSL, so that's why I'm using the
BinaryWrite approach. Thanks...


Regards,

Gregory Silvano
 
G

Gregory Silvano

Sorry, forgot to mention that this uses version 1.0 of the .NET
Framework with SP1 installed.

Thanks...
 
P

Patrice Scribe

Looks even a bit weird you can click several times. My first try would be to
see how it behaves when writing the gif file in a new window.

Also I would start with just the content-disposition header (in particular
doesn't clear the header, doesn't use the content length and the content
type for now, is it enough to still work ? does it still hang ?).


Patrice
 
G

Gregory Silvano

Patrice,

Thank you - it was the Content-Length that caused it to blow up.

I also needed the Response.End after the BinaryWrite, or the
downloaded file would contain all the HTML of the page appended to the
end of it.

Thanks for taking the time to help...


Greg
 
G

Gregory Silvano

Thanks George,

While Response.End didn't solve the initial problem, it did solve the
issue that every downloaded file contained the page's HTML appended to
the end of it.


Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top