通过按钮单击保存PhpSpreadSheet
发布时间:2020-12-13 18:28:59 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试让我的Laravel应用程序下载一个带有 phpSpreadSheet延续PhpExcel的excel文件.但到目前为止,我没有任何运气.我首先尝试通过onClick进行Axios调用,但由于不允许JS保存,因此无效.之后我尝试将按钮附加到Laravel动作,这只是打开一个空页面. 我不知道这
我正在尝试让我的Laravel应用程序下载一个带有
phpSpreadSheet延续PhpExcel的excel文件.但到目前为止,我没有任何运气.我首先尝试通过onClick进行Axios调用,但由于不允许JS保存,因此无效.之后我尝试将按钮附加到Laravel动作,这只是打开一个空页面.
我不知道这里是否有人能够帮助我,但我会保持充满希望
首先,您需要在路由中设置端点以使用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 = PhpOfficePhpSpreadsheetIOFactory::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>会工作. 希望这对你有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |