I'm trying to export a CSV using the Laravel Excel Package https://docs.laravel-excel.com. I need to pass a few variables in using an ajax request which is all working but the response isn't downloading the CSV.
I'm using the package in a few places without ajax just a simple export and it's all working so I know that side is all good.
Here is the ajax I'm using to send the data:
$(document).ready(function() {
$( "#export" ).on( "click", function() {
let clubInfo = $("#clubInfo select").val();
let funderInfo = $("#funderInfo select").val();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: 'post',
url: '{{route('export')}}',
data: {club : clubInfo, funder: funderInfo},
}).done(function(response) {
console.log(response);
}).fail(function() {
console.log("Failed");
});
});
});
Here is the controller that handle the export:
public function adminExport(Request $request)
{
$club = $request->input('club');
$funder = $request->input('funder');
return Excel::download(new UsersExport($club, $funder), 'users.xlsx');
}
I'm not getting any PHP errors or warnings which is all good I'm just getting a response in the network tab which looks like it's the csv it's just not downloading. The headers of the response are:
Accept-Ranges: none
Cache-Control: public
Connection: keep-alive
Content-Disposition: attachment; filename=users.xlsx
Content-Length: 6812
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet