I've implemented the ajax download CSV functionality, for that I've used below code. But it's not working. The code is working fine if I use it in plain PHP code but when I use it in AJAX code, the file is not downloading but it's returning values.
Return Data
bla_col1, bla_col2, bla_col3
data1, data2, data3
Ajax Code
$.ajax({
url: base_url + "/product/download-csv",
type: 'get',
data: {category: category},
success: function (response) {},
error: function (err) {},
complete: function (result) {
}
});
Laravel / PHP Code
$filename = "my-file.csv";
$fp = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=' . $filename);
fputcsv($fp, $csvHeadings);
foreach ($csvValues as $csvValue) {
fputcsv($fp, $csvValue);
}