Peter Evjan
Peter Evjan

Reputation: 2433

Downloading a file over https in IE8, using ASP.NET

I'm trying to make it possible for the user to download an Excel spreadsheet from our site, by having a button that redirect through this:

Response.Redirect(string.Format("../excel/ExcelForm.aspx?pathName=&fileNameDisplay={0}&fileNameUnique={1}", "spreadsheet.xls", fileName));

The aspx page just sends back the file through the Response object, like this:

 Response.ContentType = "application/vnd.ms-excel";
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay);
 Response.WriteFile(Server.MapPath(pathName + fileNameUnique));
 Response.Flush();
 Response.End();

Everything works just fine on my machine, but when we're putting it on the server, the https in combination with no-cache settings gives us an error saying "Internet Explorer cannot download [blahblahblah]". The cache settings on the page displaying the excel button:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("cache-control", "private, no-cache, must-revalidate no-store pre-check=0 post-check=0 max-stale=0");
HttpContext.Current.Response.Cache.SetNoServerCaching();

When I remove those lines, everything works just fine. However, I'm not allowed to remove them for other reasons. So I tried adding the following line to the ExcelForm.aspx just before it adds stuff to the header:

Response.ClearHeaders();

Which just gives me "Internet Explorer cannot download ExcelForm.aspx from [url]". And that's where I'm stuck. Suggestions?

Upvotes: 8

Views: 11456

Answers (3)

Yannick Richard
Yannick Richard

Reputation: 1249

I had the exact same issue I was unable to download a binary stream trough IE8

According to the information on this page, my new code looks like

  • Response.ClearHeaders();
  • Response.ContentType ="application/octet-stream";
  • Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}", "nameofthefile.exe"));
  • Response.BinaryWrite(bytes);
  • Response.End();

and now it works like a charm under all browsers

Upvotes: 0

Vinod T. Patil
Vinod T. Patil

Reputation: 2961

I also faced the same issue,

When I googled it, I found that "no chache" settings in response header i.e. following code is the reason for the issue.

Response.AppendHeader("Pragma", "no-cache") 
Response.AppendHeader("Cache-Control", "no-cache") 
Response.AppendHeader("max-age", "0") 

Some of the blogs says, to fix this issue, you should do some modification in Windows registry on Webserver and on all client machines(:O), well it is not feasible to do registry settings on every client machine.

The root cause is no-cache settings in response header, so I just added

Response.ClearHeaders() 

before adding content to be downloaded to the response header. The code is as shown below,

Response.ClearHeaders() 
Response.ContentType = "application/ms-excel" 
Response.AppendHeader("content-disposition", "attachment; filename=""" + fileName + """") 
Response.BinaryWrite(fileBytes) 
Response.End() 

It has fixed the issue.

Enjoy!!!

Upvotes: 3

Jamie Dixon
Jamie Dixon

Reputation: 53991

I had a similar problem myself recently when exporting CSV files from an MVC controller method. I found that adding:

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

Solved the problem for me in IE

Hope this helps!

Upvotes: 15

Related Questions