I’ve recently developed a web user control that lists a series of reports. When the user clicks on the report it serves back a CSV file download in the response stream using the following code:
Response.Clear();
Response.ContentType = "text/CSV";
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;
Response.AddHeader("Pragma", "must-revalidate");
Response.AddHeader("Cache-Control", "must-revalidate");
Response.AddHeader("Accept-Header", csvResults.Length.ToString());
Response.AddHeader("Content-Length", csvResults.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=test.csv");
Response.Write(csvResults.ToString());
Response.Flush();
Response.End();
The code initially worked fine in all browsers. Then the client put a requirement to use SSL for the site. As part of this I introduced a global handler to update the protocol from HTTP to HTTPS for all requests as follows:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string requestURL = Request.Url.ToString().ToLower();
if (requestURL.StartsWith("http://"))
{
Response.Redirect(requestURL.Replace("http:", "https:"));
}
}
However, since securing the site using SSL the CSV file downloads no longer work for IE although they do continue to work for Firefox / Chrome / Safari.
Is there something I am missing in the headers that is unique to IE in order for the file response to work correctly?
The message I receive from IE is:
“Internet Explorer cannot download
Reports.aspx from …. in ……Internet Explorer was unable to open
this Internet site. The requested site
is either unavailable or cannot be
found. Please try again later.”
UPDATE:
Here is some example fiddler output coming back from the page request which looks like it’s serving correctly. Why doesn’t IE understand that it’s just been served a file?
HTTP/1.1 200 OK
Date: Tue, 09 Nov 2010 14:23:50 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Pragma: no-cache
Pragma: must-revalidate
content-disposition: attachment; filename="test.csv"
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Length: <length value would be here>
Content-Type: text/CSV
"COL1","COL2","COL3"
"VAL1","VAL2","VAL3"
"VAL1","VAL2","VAL3"
"VAL1","VAL2","VAL3"
After spending hours trying to resolve this I’ve finally came up with a solution.
Luckily I managed to come across a post by Eric Law regarding IE’s incompatibility between HTTPS and response headers containing cache directives.
Having now made sure that all response headers were cleared and that no caching instructions were being performed on the response then the file started responding as a download again.