【问题标题】:Laravel-Excel xlsx download request not workingLaravel-Excel xlsx 下载请求不起作用
【发布时间】:2018-03-16 20:08:52
【问题描述】:

我正在使用 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");
     }
}

即使响应头看起来没问题,浏览器也不会下载文件。

谁知道我做错了什么?

提前致谢。

【问题讨论】:

    标签: php http-headers laravel-5.1 xlsx


    【解决方案1】:

    您不能只从 ajax 请求中下载东西。您的前端可能应该执行一些额外的步骤,例如创建包含该调用内容的链接。

    编辑

    我不能专门为你写一些代码,因为我不知道前端是如何制作的。但是你可以使用一个库作为https://github.com/eligrey/FileSaver.js/

    例如,当您为电子表格下载一些二进制数据时:

    http('yourBackendUrl').then(function(response) {
    
        var blobData = new Blob([response.data], {type: "application/xlsx"})
        saveAs(blobData, filename+'.xlsx')
    
    }
    

    【讨论】:

    • 你能放一些代码来解释你的怀疑吗?
    • 为我工作!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2015-09-08
    • 1970-01-01
    • 2015-11-13
    • 2019-01-27
    • 2015-01-29
    • 2019-04-08
    相关资源
    最近更新 更多