首先,您需要在路由中设置端点以使用ajax(在您的情况下为axios)调用它:
Route::get('spreadsheet/download',[
'as' => 'spreadsheet.download',
'uses' => 'SpreadsheetController@download'
]);
在你的控制器中:
public function download ()
{
$fileContents = Storage::disk('local')->get($pathToTheFile);
$response = Response::make($fileContents, 200);
$response->header('Content-Type', Storage::disk('local')->mimeType($pathToTheFile));
return $response;
}
如果您没有该文件,可以将其保存到php://output:
public function download ()
{
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="file.xlsx"');
$writer->save("php://output");
}
现在你只需要调用端点/电子表格/下载来开始下载,但是正常的< a href =“/ spreadsheet / download”>下载< / a>会工作.
希望这对你有所帮助.