Laravel-Excel xlsx 下载请求无效
Laravel-Excel xlsx download request not working
我正在使用 Laravel 中的 Maatwebsite/Laravel-Excel(2.1 版)库下载 xls 文件。
class ControllerApi extends Controller {
private $excel;
public function __construct(Excel $excel) {
$this->excel = $excel;
}
public function getXlsFile(Request $request) {
$this->excel->create("Report2016", function($excel) {
// Set the title
$excel->setTitle('My awesome report 2016');
// Chain the setters
$excel->setCreator('Me')->setCompany('Our Code World');
$excel->setDescription('A demonstration to change the file properties');
$data = [12,"Hey",123,4234,5632435,"Nope",345,345,345,345];
$excel->sheet('Sheet 1', function ($sheet) use ($data) {
$sheet->setOrientation('landscape');
$sheet->fromArray($data, NULL, 'A3');
});
})->export("xlsx");
}
}
即使响应 header 似乎没问题,浏览器也不会下载文件。
有人知道我做错了什么吗?
提前致谢。
您不能仅通过 ajax 请求下载内容。您的前端可能应该执行一些额外的步骤,例如使用该调用的内容创建 link。
编辑
我无法专门为您提供一些代码,因为我不知道前端是如何制作的。但是您可以将库用作 https://github.com/eligrey/FileSaver.js/.
例如,当您为电子表格下载一些二进制数据时:
http('yourBackendUrl').then(function(response) {
var blobData = new Blob([response.data], {type: "application/xlsx"})
saveAs(blobData, filename+'.xlsx')
}
我正在使用 Laravel 中的 Maatwebsite/Laravel-Excel(2.1 版)库下载 xls 文件。
class ControllerApi extends Controller {
private $excel;
public function __construct(Excel $excel) {
$this->excel = $excel;
}
public function getXlsFile(Request $request) {
$this->excel->create("Report2016", function($excel) {
// Set the title
$excel->setTitle('My awesome report 2016');
// Chain the setters
$excel->setCreator('Me')->setCompany('Our Code World');
$excel->setDescription('A demonstration to change the file properties');
$data = [12,"Hey",123,4234,5632435,"Nope",345,345,345,345];
$excel->sheet('Sheet 1', function ($sheet) use ($data) {
$sheet->setOrientation('landscape');
$sheet->fromArray($data, NULL, 'A3');
});
})->export("xlsx");
}
}
即使响应 header 似乎没问题,浏览器也不会下载文件。
有人知道我做错了什么吗?
提前致谢。
您不能仅通过 ajax 请求下载内容。您的前端可能应该执行一些额外的步骤,例如使用该调用的内容创建 link。
编辑
我无法专门为您提供一些代码,因为我不知道前端是如何制作的。但是您可以将库用作 https://github.com/eligrey/FileSaver.js/.
例如,当您为电子表格下载一些二进制数据时:
http('yourBackendUrl').then(function(response) {
var blobData = new Blob([response.data], {type: "application/xlsx"})
saveAs(blobData, filename+'.xlsx')
}