rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.09k stars 246 forks source link

Download on Ajax Request #295

Open dev-aes opened 1 year ago

dev-aes commented 1 year ago

is there any solution to this problem? I don't see any guides about downloading sheet files thru ajax request ( Axios)

conlacda commented 1 year ago

I have the same problem.

conlacda commented 1 year ago

@dev-aes You can refer to my code snippet. Here I save the file to storage_path and then send it to the client. PHP code

$fastexcel = new FastExcel($collection);
$fastexcel->export(storage_path("temp_file.csv"));
return response()->download(storage_path("temp_file.csv"), "temp_file.csv")->deleteFileAfterSend(true);

Js code

axios({
    url: 'export-csv',
    method: 'post',
    responseType: 'blob',
    data: {},
}).then((response) => {
    let url = window.URL.createObjectURL(new Blob([response.data]));
    let link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', "temp_file.csv");
    document.body.appendChild(link);
    link.click();
});