Laravel Excel 下载不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48961837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Excel download not working
提问by Mango D
The problem is the same as described here Laravel Excel Download using Controller
问题与此处描述的相同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.
但我无法相信没有其他方法可以在不使用其他资源的情况下处理 Laravel 中的 Excel 下载。我已经能够在控制器中response()
处理 PDF的即时下载。
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.
所以excel文件被创建并正确保存在我的存储文件夹中(发生在上面的代码之前)。然后我使用一种axios.get
方法下载具有上述功能的文件。
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
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.
但是无论我做什么或尝试更改下载都无法启动。
采纳答案by Mango D
I manged to solve it.
我设法解决了它。
It seems to be not possible to solve this with a simple axios.get
request to the route. Instead I needed to open the route directly with a link.
似乎不可能axios.get
通过对路线的简单请求来解决这个问题。相反,我需要直接使用链接打开路线。
Did not work with:
不适用于:
HTML
<button @click="downloadExcel()">Download Table as Excel File</button>
HTML
<button @click="downloadExcel()">Download Table as Excel File</button>
downloadExcel() {
axios.get('/customers/export');
}
Simple solution (instead of just using axios.get
):
简单的解决方案(而不是仅仅使用axios.get
):
<a :href="/customers/export"><button>Download Table as Excel File</button></a>
So it would also be possible to open the route after the axios request with:
所以也可以在 axios 请求之后打开路由:
downloadExcel() {
let newWindow = window.open();
axios.get('/customers/export')
.then(response => {
newWindow.location = 'http://' + window.location.hostname + '/customers/export';
});
}
回答by Pratik Mehta
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,
谢谢,