问题
I was trying to implement a "download link" and put it beside one of my report table so that users can download a csv file and open it with applications like Excel.
The records are generated dynamically based on the query made by users.
So somewhere in my controller there's something like:
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=xxx.csv'
return response.stream(dynamically_generated_csv, request=request)
This works in both FireFox & Chrome, but fails in IE.
When I print out the response headers, I found that several headers were added to my response by web2py:'Expires', 'Cache-Control', etc...
And when I remove the 'Cache-Control' header by doing the following:
del response.headers['Cache-Control']
It works in IE.
So it seems like IE has trouble dealing with a downloadable file with 'Cache-Control' set to certain value.
Now, my question is:
Why does web2py add these response headers, implicitly? and maybe without a way to set it off?
is there any side effect when i delete the 'Cache-Control' header this way?
Thanks in advance.
回答1:
I'm not sure what Cache control headers are/were being sent but IE has a bug with downloaded files like you are experiencing.
For IE, you MUST enable caching. When IE loads files (e.g. Excel files), in Excel, it loads them from the cache directory, thus if you don't cache it, Excel (or your other app) will fail to load the file.
Eric Law (MSFT) on the topic: http://blogs.msdn.com/ieinternals/archive/2009/10/02/Internet-Explorer-cannot-download-over-HTTPS-when-no-cache.aspx
Update: If however you just want to force the download... e.g. not have IE load the excel file inside the IE window... then be sure to set the full headers for the attachment.
//PHP style
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
回答2:
Is the download link using https (ssl)? If so, then IE can't handle the downloading if it's set to be cached. This is a known problem with IE.
回答3:
This doesn't answer your question, but solves, I hope, original problem.
I would just add timestamp (something unique enough) to query string of link to CSV file. It helped for me.
来源:https://stackoverflow.com/questions/1999950/download-link-fails-in-ie