views:

1106

answers:

8

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.

A: 

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

Dominik
+3  A: 

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.

Jean-Philippe
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."\"");
Steve
+2  A: 

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.

Seth
I had this originally, no joy.
tau-neutrino
A: 

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.

dev-null-dweller
+1  A: 

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"
Jason
A: 

Did you ever resolve this? What ended up being the solution to what you were seeing?

Greg
A: 

Does anybody have an answer for this? I've tried all these solutions as well as every solution on the net. Nothing fixes this MS bug!

dan
See the answer I just posted for how we got this working.
jasonbar
+1  A: 

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));
jasonbar