views:

636

answers:

2

I'm reviewing some legacy code and I've found a bug that causes the response to sit indefinitely.

Here's the basic idea:

Response.Content-Type = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename" & someFileName)
Response.AddHeader("Content-Length", someStoredLength)
Response.BinaryWrite(someByteArray)
Response.Flush()
Response.End()

The problem is that someStoredLength is much larger than the actual size of someByteArray, so the client just sits there waiting for the file download while the browser just spins.

I'm contemplating just removing the AddHeader that specifies the content length, because when I do that everything seems to work fine, but I'm worried that I'm not understanding something.

Is it ok for me to remove this AddHeader or should I figure out a better way to deal with this problem?

+5  A: 

Your application SHOULD (scroll down to Content-Length) define it, however, it's not strictly required.

Here's a decent discussion of possible options.

Ben S
+4  A: 

Is there a reason you can't just change the content-length line to the following:

Response.AddHeader("Content-Length", someByteArray.Length)

Seems like that would be the way to go.

Skinniest Man
I was thinking of doing that too. I was wondering if that would be a good alternative. If I have a byte array will the Length property always give me the correct size?
Joseph
Yes. The content-length header indicates the number of bytes in the content. Your content is an array of bytes, so you're good.
Skinniest Man