.NET Bug with BinaryWrite or just bad code?


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"), _

'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))

'I've also tried Response.Clear and .ClearContents

Response.ContentType = "application/octet-stream"

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


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...


Gregory Silvano

Gregory Silvano

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


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 ?).


Gregory Silvano


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...


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.


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

Latest member

Latest Threads