views:

311

answers:

1

I use the Ajax JQuery command to call a PHP script that generates a CSV file and returns a link to that file for the user to download.

I would like to make this more user friendly by automatically starting the download so the user sees the browsers "Save or Open" window instead of having to click the download link.

I'm guessing I need to somehow change the headers in the Ajax success callback function??

I'm not really sure what I should be titling my searches or even if this is possible.

Thanks!

+1  A: 

You can do this easiest (and possibly only) server-side, no need for ajax, like this:

<?php
header('Content-type: "text/csv"; charset="utf8"'); //adjust encoding if needed
header('Content-disposition: attachment; filename="fileNameHere.csv"');
//output document in response
?>

Someone feel free to edit this if the syntax if off, it's been quite a while since I've had a php project.

Nick Craver
Thanks! This got me on my way to getting it right!
krio