Internet Explorer and file download on HTTPS
Getting Internet Explorer to download a file over HTTPS (ie. Word, PDF) can be pretty frustrating because of a bug in IE6 (some people reported that this bug is still there in IE7; i could not test this). Instead of downloading the file IE gives me an error message “Could not write file to cache” (actualy it is “Die Datei konnte nicht in den zwischenspeicher geschrieben werden”; i do not have an english installation of IE around for testing). Note that this only occurs with HTTPS while HTTP works fine.
Comment nr. 24 by Kuroiryu in Joseph Scotts blog explains why things are not working as expected:
The “no-cache” and “no-store” options are telling IE to get rid of the file once it’s been launched in Excel. The problem is that IE doesn’t actually KNOW when Excel launches the file. Since newer versions of Office are slow to start up, the delay isn’t great enough, so by the time Excel is ready to open the file, IE has already deleted it. Joseph’s solution works because the only cache limiter he’s using is “public”
There are various proposals on how to solve this problem (basicaly variations of the “Content-Disposition”, “Pragma” and “Cache-Control” headers) and none of these solutions seem to work for everyone. So i’ll add my solution to the top and let you try out if it works
response.setContentLength(12345);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", attachment; filename=foo.pdf);
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
Most important is to use setHeader() (which removes other values for this key) instead of addHeader() (which adds values to this key). This piece of code is java but all the other languages offer similar methods for manipulation of response headers.
Here are some resources that helped me:
iyujfa said,
May 23, 2007 @ 5:30 pm
Hello! Cool guestbook, interesting information. Keep it UP.
I’ll be back.
Boaz said,
May 24, 2007 @ 10:48 am
Thank you. It worked.
Grace said,
October 12, 2007 @ 7:09 pm
Very nice and detailed information. And it worked!
I just have one question. Do you know by setting Pragma to be public, it will potentially have any security concern?
Admin said,
October 12, 2007 @ 7:20 pm
Hi Grace
Check out http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.
The “public” value is not even defined. The only defined value is “no-cache”. I’ll tried different combinations (if not all) of the headers to get things running.
have a nice weekend
pascal
narayana said,
October 18, 2007 @ 1:49 pm
Hi,
I am really thank full to you. After a lot of digging in the net. I got this. It is really working.
Thanks alot.
Florian said,
April 14, 2008 @ 5:52 pm
Thank you so much for this tip, i searched long for a solution to this problem!!
Itay Merchav said,
April 22, 2008 @ 10:00 am
Thanks, the ’set’ instead of ‘add’ header solved the prob.
Helio Frota said,
April 29, 2008 @ 8:16 pm
Thanks, it really works and solve my problems with IE7 !
Lars said,
July 21, 2008 @ 3:53 pm
Works fine here, too.
Chris said,
September 23, 2008 @ 10:44 am
Thanks,
For asp.net solution, Response.ClearHeaders() is needed before, because is not ’set’.
Greetings
Matei Nick said,
October 27, 2008 @ 12:44 pm
a added this 2 lines in my servlets, and now is working download documents in servlets but i have and some pdf files (disk stored) that i can’t acceess in https mode. have any ideeas?
response.setHeader(”Pragma”, “public”);
response.setHeader(”Cache-Control”, “max-age=0″);