File download failure in Internet Explorer

IE is weird. Still, many people prefer to use IE. Recently I had an issue with csv file download. I have a program that downloads a set of data from the database in the form of csv file. If you click the download button, you will be prompted to open/save the csv file. All is well in firefox, safari and chrome. When I test this in IE, it bombs and throws error message “Internet explorer can’t contact the site….”

The page I was testing was on the https site. It also use session variables. For some reasons IE goes haywire when it looks at session variables, https and file download request combo. After banging my head on the wall for few minutes, I found the blog written by Joseph Scott. Joseph listed the ways to fix this issue. However, that didn’t work for me.

Fortunately lots of users pitched in and left their comments about what really worked for them. I tried all of them in sequence, nothing seems to be working. One of the users, Duncan, posted the code to check for IE and execute different header commands. Following is the working code.


header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=filenamegoeshere.csv');

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
{
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
}
else
{
header('Pragma: no-cache');
}
//Now echo the content of csv file

We basically force IE to download the file from server instead of cache. That did the trick and IE behaved.
икони

Related Posts Plugin for WordPress, Blogger...
Bookmark and Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>