Laravel Excel download not working

Mango D picture Mango D · Feb 24, 2018 · Viewed 8.5k times · Source

The problem is the same as described here Laravel Excel Download using Controller

But I just can not believe that there is no method to deal with Excel downloads in Laravel without using another resource. I was already able handle the instant downloads in controller with response() for PDFs.

Mabybe the headers are wrong? My code:

public function getFile($file) {
    $path = storage_path('app/excel/exports/' . $file);

    $headers = array('Content-Type' => File::mimeType($path));

    return response()->download($path, $file, $headers);
}

So the excel file is created and saved correctly in my storage folder (happens before the code above). Then I use an axios.get method to download the file with the function above.

Headers I am getting:

Accept-Ranges:bytes Cache-Control:public Connection:keep-alive Content-Disposition:attachment; filename="test_file.xlsx" Content-Length:7066 Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

But whatever I do or try to change the download just won't start.

Answer

Pratik Mehta picture Pratik Mehta · Feb 24, 2018

You can try with these two headers.

return response()->download($path, $file, [
     'Content-Type' => 'application/vnd.ms-excel',
     'Content-Disposition' => "attachment; filename='Report.xls'"
]);

Thanks,