Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get csv file to download on IE? Works on firefox

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.

like image 610
tau-neutrino Avatar asked Feb 09 '10 19:02

tau-neutrino


People also ask

How do I open a CSV file in Internet Explorer?

Via file Explorer View/Options/File Types. Look for CSV file type click Open.

Can Firefox open a CSV file?

Firefox allows you easily export your saved logins and passwords to a CSV file. It includes the appropriate option in the Logins and Passwords tool, which is also known as Lockwise Password Manager. However, there is no import option visible by default.

Can I open CSV file in browser?

Click "File" and "Open," then use the Explorer window to locate the HTML file you want to add the CSV data to. Click the “Open” button.


1 Answers

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.

like image 98
Caissy Avatar answered Sep 18 '22 03:09

Caissy