views:

272

answers:

1

I am running a website on IIS6 for ASP.NET application and enabled compression, which worked fine for the .aspx web pages.

What isn't working is downloaded binary files that are transmitted as part of a postback response: e.g. link button is 'download to excel' - user clicks and the code generates a binary file, does a Response.Clear() and then writes the file to the Response.OutputStream.

A response is seen by browsers but it is always zero bytes. I assume therefore that web browsers are expecting a compressed response, and as the raw binary isn't a valid compressed stream it fails. I'm puzzled as to why this should be if I've cleared the response - surely the response headers (specifying compression) are also cleared?

So two questions arise:

1) how do I compress the binary file to send a compressed response? 2) how can I check at runtime to see if IIS compression is enabled?

Cheers

+3  A: 

I would disable compression and check whether this still works, just to isolate the fact that this is indeed due to IIS compression. I'm telling you that 'cos I'm running a IIS/Compression enabled site which provides PDF files (binary) without a problem.

Anyway here's the part of code which works for me.

 Response.Clear();
 Response.ClearHeaders();

 Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
 Response.AddHeader("Content-transfer-encoding", "8bit");
 Response.AddHeader("Cache-Control", "private");
 Response.AddHeader("Pragma", "cache");
 Response.AddHeader("Expires", "0");  

 Response.ContentType = "application/pdf";
 Response.WriteFile(filePath);

 Response.Flush();
 Response.Close();
 HttpContext.Current.ApplicationInstance.CompleteRequest();
Uchitha