In my application user can download a file after clicking a link. Document is PDF/RTF generated in code. I use:
byte[] downloadBytes = some pdf document bytes...
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition",
"attachment; filename=filename.pdf; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End();
It works OK but is this generally a good way ? Why flush is called two times? I found many different examples and this one works fine but sometimes I got The remote host closed the connection. The error code is 0x80070057
error. I found solution that I should use
if (Response.IsClientConnected)
{
Response.Flush();
Response.End();
}
How the entire code should look like?