views:

185

answers:

5

Hi,

I'm giving a user a link to download a csv file ...just using

Click <a href="report.csv">here</a> to download your file.

This text and link is being displayed in a small popup window - height 100 width 400.

If the user clicks "save" - then no problem then the file is saved to wherever they choose

If though they choose "open" the csv file is then displayed in the small popup window. Which isn;t what I want - I'd prefer excel to open and display the file in excel, or even disabling the "open" button may be a possible option.

Any ideas how I can achieve either?

Thanks,

A: 

I believe this is OS-level and can't be changed.

ceejayoz
+1  A: 

You have no power over users' settings such as which file type is opened by which application. They would have to go to their settings and change the file association. However, bear in mind not everyone has Excel installed, so it's not a good idea to force the user to open the csv file in this particular application.

I also don't think you can disable the Open option, this is also dictated by the web browser's configuration whether it will or will not display the window or will it launch the associated application directly etc... it seems you're out of luck in your particular case.

Peter Perháč
+3  A: 

I assume that you are opening that popup box using some javascript .. so I'd suggest to dont show/open your tiny "download" popup box and instead show this link on a webpage. So when somebody clicks on the link force the browser to download the file and if they wish to open it they can and probably get a view which is as wide as of browswer's windows size.

// just for example, to force download of a zip file, send the headers as

// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
Wbdvlpr
actually...this was pretty close. I just added..$strData is my stringfunction csv_export($strData){$myFile = "report.csv";header("Cache-Control: public");header("Content-Description: File Transfer");header("Content-Disposition: attachment; filename=".$myFile);header("Content-Type: application/octet-stream'");$fh = fopen($myFile, 'w') or die("can't open file");fwrite($fh, $strData);fclose($fh);readfile($myFile);}?>
thegunner
A: 
  • Make sure you don't do any other output before this point
  • Build your csv string and store it in $csvString
  • Give it the appropriate filename in $csv_filename
  • The headers should tell the browser to load it in excel

    header("Cache-Control: maxage=1");

    header("Pragma: public");

    header("Content-Type: application/vnd.ms-excel");

    header("Expires: 0");

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

    header("content-disposition: attachment;filename=$csv_filename");

    echo $csvString;

Jim Ford
The MIME type for CSV files is text/css, not application/vnc.ms-excel: http://www.rfc-editor.org/rfc/rfc4180.txt
David Dorward
Thanks got it working....close enough for me!
thegunner
it is, but he stated he wanted it to open in excel. text/csv would work as well.
Jim Ford
It's been awhile since I used this code snippet, I remember something about an issue with one of the IE version that was fixed by changing from text/csv to application/vnd.ms-excel.
Jim Ford
A: 

One half-solution is adding target="_blank" to the tag. That way, when users select the 'open' option, at least the file will be opened in a new (full-screen, or at least resizable) window.

Other than that, you can't change the behaviour of the window, as ceejayoz stated.

Duroth