34

I'm struggling with an odd error. I have a simple web app that grabs stuff from a DB then outputs it as a downloadable csv file. It works on firefox and chrome, but IE fails to recognize it as a csv file (thinking it is a html fle) and when I click save I get the error, "Unable to download {name of file} from {name of site}. Unable to open this internet site. ..."

Code:

session_start();

//some logic goes here...  

//generate csv header  
header("Content-type: application/octet-stream");  
header("Content-Disposition: attachment; filename=exportevent.csv");  
header("Pragma: no-cache");  
header("Expires: 0");  

echo "Event: " . $event_title . "\n";  

//print the column names  
echo "Last Name, First Name, Company \n";  

while($row = mysql_fetch_assoc($result))  
{  
    echo $row['atlname'] . ',' . $row['atfname'] . ',' . $row['atcompany'] . "\n";      
}

I've played around with the content-type a whole bunch, but that had no effect.

Update: I've tried text/csv, application/vnd.ms-excel (and variations of this), text/plain, and some others that I now forget with no luck.

This is IE8 btw.

Update 2: The connection is over SSL.

1
  • 2
    what other content types did you try? I typically use text/csv Feb 9, 2010 at 20:00

12 Answers 12

56

Don't we love IE? :)

Try using those headers:

  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: private",false);
  header("Content-Type: application/octet-stream");
  header("Content-Disposition: attachment; filename=\"exportevent.csv\";" );
  header("Content-Transfer-Encoding: binary"); 

I think that the octet-stream content type forces IE to download the file.

3
  • 2
    I've found the minimum headers for FF 3.6 and IE 8 are: header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"".$filename."\"");
    – Ben
    Oct 15, 2010 at 7:38
  • 5
    <sarcasm>Oh yeah IE I'm loving it</sarcasm>
    – Ardee Aram
    Jan 28, 2011 at 4:24
  • Ah, throw out an old Excel plugin and roll your own CSV creator - IE8 barfs all over your download. Thank you :]
    – phatskat
    Sep 19, 2012 at 5:48
6

We recently ran into this problem ourselves. See this MSKB article

These are the headers we ended up having to use to get it to work over SSL.

header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file_name\";");
header("Content-length: " . strlen($csv_string));
2
  • Tried several of these and various combinations. Using RegEdit did the trick. Thanks!
    – marklark
    Aug 23, 2011 at 21:06
  • Doesn't the multiple Cache-Control headers causes only the second header to be sent?
    – Ross
    Feb 18, 2015 at 3:05
4

I've had success with the following:

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=File.csv");

Setting the type to application/vnd.ms-excel seemed to do the trick in my case. This is all in a file that is opened by submitting a form using

target="_blank"
4

The only extra code I had to add for IE to work with SSL was: header("Pragma: public"); So my headers look like this now:

 header("Pragma: public");  
 header("Content-Type: application/octet-stream");
 header("Content-Disposition: attachment; filename=some_filename.csv");
1
  • header("Pragma: public"); worked well for me also with ssl. Thanks Jun 21, 2011 at 14:48
2

We have just had the same issue and after adding many headers and getting a working link I then removed them one by one and found the key one for us was "Cache-Control: public" so in the end we just had

header("Cache-Control: public"); 
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");

which worked fine.

0
1

Try setting your content type to text/csv instead of application/octet-stream.

Since application/octet-stream is a generic binary mime type (and doesn't match the '.csv' extension), Internet explorer might be ignoring it and computing the mime type based on the file extension.

1
  • I had this originally, no joy. Feb 9, 2010 at 22:03
0

After using Javascript it will solve your problem.

Use this for IE,

    var IEwindow = window.open();
    IEwindow.document.write('sep=,\r\n' + CSV);
    IEwindow.document.close();
    IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
    IEwindow.close();

For more information i have written tutorial on that, see - Download JSON data in CSV format Cross Browser Support

Hope this will be helpful for you.

0

The solution for me was:

header_remove(); 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment; filename=brokerlist.csv'); 
echo $content;
-1

Did you try the Content-type: text/csv ?

-1

Some time ago I've got a problem with IE6 opening pdf files, and crashing when AdobeReader 6.0 was installed and tried to open file in browser window. Than I found somewhere this header:

header('Content-Type: application/force-download');

And it solved the problem, every pdf file was downloaded and opened in Adobe instead of IE.

-1

This simply doesn't make sense. I tried the accepted answer, all the other answers in here, and it didn't work for me. I tried their permutations, and somehow I managed to make it work in IE like so:

 header("Pragma: public");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Cache-Control: public");
 header("Content-Type: application/vnd.ms-exce");
 header("Content-Disposition: attachment; filename=coupons.csv" );
 header("Content-Transfer-Encoding: binary");       
 header("Content-Length: " . strlen($csv)); 
 echo $csv;
 die();

One thing I did is to empty the cache every freaking time I test the code. And it still doesn't make sense. Just in case someone might need this desperately ;)

2
  • I think it should be "application/vnd.ms-excel". excel with an "L"
    – Dave
    Aug 15, 2011 at 17:55
  • Depending on the end user platform the "Content-Type: application/vnd.ms-exce" will not work on Mac and Linux clients. As long as your page visitors all run Windows you are ok. Sep 21, 2012 at 14:07
-2

If you are trying to accomplish this task (getting a CSV file to download in IE8) using Salesforce.com (in which case your front-end is Visualforce and you can't set all of the headers, only some of them), here's what you need:

<apex:page cache="true" 
        contentType="application/octet-stream#myAwesomeFileName.csv" 
        showHeader="false" sidebar="false" standardStylesheets="false">
     <apex:outputText value="{!csvContent}" escape="false"/>
</apex:page>

The key pieces here are cache=true, which, in conjunction with the default expires=0 attribute, achieves the following headers:

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");

And then the contentType of application/octet-stream --- doing text/csv fails for IE8.

Not the answer you're looking for? Browse other questions tagged or ask your own question.