-1

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
  • These are the headers as a ajax response. You will have to trigger the download on your own. If the binary data of excel sheet is coming in the response, then you can create an object URL in javascript, set this URL to an anchor tag and then enable a click on it for download. – nice_dev Aug 14 '19 at 10:54
  • Don't use ajax in this case simple submit form with normal php, it'll download. – Dilip Hirapara Aug 14 '19 at 10:55

2 Answers2

0

Unless fetching the file through AJAX is a super hard requirement, I would suggest using window.open('your_url/your_route?club=XXXXX&funder=XXXXX', '_blank'); to download the file and avoiding any headaches.

0

I think you need to inform the type of export. Note the following:

return (new InvoicesExport)->download('invoices.csv', \Maatwebsite\Excel\Excel::CSV);

But in your controller, I can't see the export type:

return Excel::download(new UsersExport($club, $funder), 'users.xlsx');

If you are using version 2.1, you need to do this:

Excel::create('Filename', function($excel) {
   //
})->export('csv');

Or ... what do you think about saving the export first and then exporting it like this?

$file_name = 'my_export';

// Find users first
$users = User::where([['club', $club], ['funder', $funder]])->get();

// then generate the file
Excel::create($file_name, function ($excel) use ($users) {
    $excel->sheet('Users', function($sheet) use($users) {
        $sheet->fromArray($users);
    });
})->store('csv', storage_path('excel/export'));

// Finally, download the file
$full_path =  storage_path('excel/export') . '/' . $file_name . '.csv';

return Storage::disk('public')->download($full_path, $file_name . 'csv', 
    ['Content-Description' =>  'File Transfer','Content-Type' => 'application/octet- 
      stream','Content-Disposition' => 'attachment; filename=' . $file_name . 
      '.csv']);

or maybe you would do that:

$headers = array(
    'Content-Type' => 'text/csv',
);

return Response::download($file_name . 'csv', $full_path, $headers);
Alexandre Barbosa
  • 1,194
  • 1
  • 7
  • 6