1

Not sure what I am doing wrong here, but I am passing some data to a controller action for our in house built framework, the way we do ajax calls is just a wrapper for regular ajax calls in Jquery.

CT.postSynch('report/payRollReport/createPayrollReport', {data : data}, function(data){});

The Data I am passing is two dates, which is used to create a csv file. This file would download normally IF I didn't have to select dates, that is if I hard code in the dates to select data between - the csv file will download.

How ever with above method, all I get back is a response that contains the coma delimited string, no file downloads. I am not sure why this is different: it downloads when its not an ajax call and it doesn't when it is an ajax call.

Obviously the headers are set right if it will download when its a regular link....

Any ideas on what I am should do with data if anything? to make it download the new file as opposed to spitting it out in a response object?

I then attempted to do the following:

    CT.postSynch('report/payRollReport/createPayrollReport', {data : data}, function(data){
        location.href='data:application/csv;charset=UTF-8,' + encodeURIComponent(data);
    });

But what I am downloading is not a CSV its just a "download" as chrome calls is

TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • performing an ajax call will not initiate a download. If you want it to be a download, don't use an ajax call. – Kevin B Nov 19 '13 at 17:11
  • possible duplicate of [Download CSV file using "AJAX"](http://stackoverflow.com/questions/3346072/download-csv-file-using-ajax) – Kevin B Nov 19 '13 at 17:12
  • As a security feature, Javascript cannot create a file and give it to your web browser. You have to use an alternative way such as these posts suggest: http://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side http://stackoverflow.com/questions/19771147/download-php-genereted-csv-file-though-jquery-javascript http://stackoverflow.com/questions/17564103/using-javascript-to-download-file-as-a-csv-file – MonkeyZeus Nov 19 '13 at 17:18
  • See the above OP it has been updated with an attempt @MonkeyZeus – TheWebs Nov 19 '13 at 17:19
  • @TheWebs thanks I just saw it after commenting – MonkeyZeus Nov 19 '13 at 17:20
  • @MonkeyZeus So how would I get a file name and extension in there, by all accounts this should work .... it should download a csv file ... – TheWebs Nov 19 '13 at 17:21
  • The edit in the Accepted Answer [here](http://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side) seems to explain it – MonkeyZeus Nov 19 '13 at 17:23
  • @MonkeyZeus is their any support for IE 8 ...... since IE lags behind them both ..... – TheWebs Nov 19 '13 at 17:24
  • I have never implemented a file download via Javascript because of the downright inconsistencies between web browsers. My general procedure is usually to trigger a CSV creation process (myurl.com/createcsv.php) which creates the file on the server and returns an array containing True or False and and a filename (username.date('Y-m-d H:i:s').rand().microtime().csv) in JSON, then if creation was successful then have javascript redirect the user to another URL (myurl.com/getcsv.php?name=NameFromAjaxCall), once the getcsv script has finished executing then simply delete the file from the server. – MonkeyZeus Nov 19 '13 at 17:58
  • were not saving the csv - they dont want to, they want and instant download. – TheWebs Nov 19 '13 at 18:00
  • I am confused, an instant download is a save. – MonkeyZeus Nov 19 '13 at 18:00
  • How long does the `createPayrollReport` procedure usually take? If you set headers correctly in PHP then you can simply provide an `` tag like this `Get Report` and once the script finishes then the browser will actually stay on the same page and simply start the download process. – MonkeyZeus Nov 19 '13 at 18:04
  • No most people save things to the server, like you generate a file its saved on the server, you go do something else, download the file later. this is generate the file, force the browser to download - also - not, you need date passed in via ajax. – TheWebs Nov 19 '13 at 18:04
  • I think you might be confusing technologies and protocols in terms of their intended purpose. What wrong with `Get report`? – MonkeyZeus Nov 19 '13 at 18:14

0 Answers0