ASP.NET How To Stream File To User

Solution 1:

I wouldn't call Response.Close() or Response.End().

Response.End() will stop the page execution/rendering at that point. No code following Response.End() will be run. The response is terminated at that point with no further output added to the stream.

Response.Close() is similar to Response.End(), but allows code to be executed after it is called (but no further output can be sent in the page response).

Response.Flush() will send any remaining response items to the page.

From an IIS core team member:

Response.Close sends a reset packet to the client and using it in anything other than error condition will lead to all sorts of problems - eg, if you are talking to a client with enough latency, the reset packet can cause any other response data buffered on the server, client or somewhere in between to be dropped.

In this particular case, compression involves looking for common patterns within the response and some amount of response has to be buffered by the compression code to increase the chance of finding longer repeating patterns - this part that is buffered cannot be sent to the client once you do Response.Close().

In short, do not use Response.Close().