内容简介:翻译自:https://stackoverflow.com/questions/45737485/saving-a-phpspreadsheet-through-button-click
延续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 = \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>会工作.
希望这对你有所帮助.
翻译自:https://stackoverflow.com/questions/45737485/saving-a-phpspreadsheet-through-button-click
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python编程
[美]埃里克·马瑟斯 / 袁国忠 / 人民邮电出版社 / 2016-7-1 / CNY 89.00
本书是一本针对所有层次的Python 读者而作的Python 入门书。全书分两部分:第一部分介绍用Python 编程所必须了解的基本概念,包括matplotlib、NumPy 和Pygal 等强大的Python 库和工具介绍,以及列表、字典、if 语句、类、文件与异常、代码测试等内容;第二部分将理论付诸实践,讲解如何开发三个项目,包括简单的Python 2D 游戏开发如何利用数据生成交互式的信息图......一起来看看 《Python编程》 这本书的介绍吧!
Markdown 在线编辑器
Markdown 在线编辑器
html转js在线工具
html转js在线工具