views:

8103

answers:

2

In a web application I am working on, the user can click on a link to a csv file. There is no header set for the mime-type, so the browser just renders it as text. I would like for this file to be sent as a .csv file, so the user can directly open it with calc, excel, gnumeric, etc.


     header('Content-Type: text/csv');
     echo "cell 1, cell 2";

This code works as expected on my computer (Isnt that how it always is?) but does not work on another computer.

My browser is a nightly build of FF 3.0.1 (on linux) The browsers it did not work in were IE 7 and FF 3.0 (on windows)

Are there any quirks I am unaware of ? Thanks

+9  A: 

You could try to force the browser to open a "Save As..." dialog by doing something like:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv')
echo "cell 1, cell 2";

Which should work across most major browsers.

Sean Bright
+3  A: 

You are not specifying a language or framework, but the following header is used for file downloads:

"Content-Disposition: attachment; filename=abc.csv"
gimel