Hi there Stackoverflow. I am trying to call a PHP script via AJAX that will create and download a CSV file. I know this isn't normally supposed to be done, however I would like to do it this way.
My ajax below returns the csv data as shown by this output:
$.ajax({
type: "POST",
url: "<?=site_url('front_office/get_csv/')?>",
data: {hashed_center_ids : hashed_center_ids, print_data : print_data},
dataType: "text",
success: function(response) {
console.log(response)
var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(response);
window.open(uri, 'test.csv');
}
});
The problem is that using the lines
var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(response);
window.open(uri, 'test.csv');
does not download the file as a CSV. But rather it give it no extension with the name "download". Does anybody know how can I make it so it downloads with the .csv extension? Thanks.