Laravel-excel: [BUG] 响应标头(cors)

创建于 2016-11-16  ·  3评论  ·  资料来源: Maatwebsite/Laravel-Excel

包版,Laravel 版

拉拉维尔 5.1
laravel-excel 2.1.0
barryvdh/laravel-cors 0.8.0

预期行为

我希望 laravel-excel 与 laravel-cors 配合得很好

实际行为

laravel-excel 忽略 laravel-cors 并且未在响应中设置
标题“Access-Control-Allow-Origin”和“Vary”

在下载和导出方法中 laravel-excel 不使用 Illuminate\Http\Response
这是什么破一切?

重现行为的步骤

请从请求www.example-a.com在javascript(在我的情况angularjs应用程序) www.example-b.com (服务器laravel + laraver-CORS + laravel-EXCEL)

最有用的评论

这个包不支持使用响应,因为 PHPExcel 处理实际下载。 您必须自己传递标题(第二个参数)

->download('xls', [yourheader])

所有3条评论

这个包不支持使用响应,因为 PHPExcel 处理实际下载。 您必须自己传递标题(第二个参数)

->download('xls', [yourheader])

请将此添加到文档中。

嗨,我实际上在 Lumen 项目中使用 Laravel-Excel,我正在尝试将数据导出到 excel 文件中并下载如下:

我的用户导出

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class UsersExport implements FromCollection
{
    /**
    * <strong i="8">@return</strong> \Illuminate\Support\Collection
    */
    public function collection()
    {
        return User::all();
    }
}

我的控制器

public function excel()
    {
        return Excel::download(new UsersExport(),'users.xlsx');
    } 

网页.php
$router->get('/download', 'CommunicationController<strong i="17">@excel</strong>');

当我尝试执行 excel() 函数时,我收到了这个错误

image

但是,很久以前我已经创建了一个 CorsMiddleware.php 并将其添加到我的 bootstrap/app.php 中,并且所有其他功能都非常感谢它。

应用程序.php

$app->middleware([
    App\Http\Middleware\CorsMiddleware::class
]);

CorsMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;

class CorsMiddleware
{
    /**
     * Handle an incoming request.
     *
     * <strong i="30">@param</strong>  \Illuminate\Http\Request  $request
     * <strong i="31">@param</strong>  \Closure  $next
     * <strong i="32">@return</strong> mixed
     */
    public function handle($request, Closure $next)
    {
        $headers = [
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age'           => '86400',
            'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With, api_token'
        ];

        if ($request->isMethod('OPTIONS'))
        {
            return response()->json('{"method":"OPTIONS"}', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
        {
            $response->header($key, $value);
        }

        return $response;
    }
}

我是不是忘记了什么? 从昨天开始我就一直坚持下去,我真的很想得到你的帮助:'(

在这里你可以看到我使用的 Lumen 版本以及 maatwebsite/excel:

"require": {
        "php": ">=7.1.3",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/lumen-framework": "5.7.*",
        "maatwebsite/excel": "^3.1",
        "phpoffice/phpspreadsheet": "^1.6",
        "vlucas/phpdotenv": "~2.2"
    },

谢谢!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

muhghazaliakbar picture muhghazaliakbar  ·  3评论

contifico picture contifico  ·  3评论

matthewslouismarie picture matthewslouismarie  ·  3评论

disto picture disto  ·  3评论

gamevnlc picture gamevnlc  ·  3评论