Laravel-excel: [BUG]応答ヘッダー(cors)

作成日 2016年11月16日  ·  3コメント  ·  ソース: Maatwebsite/Laravel-Excel

パッケージバージョン、Laravelバージョン

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に(私の場合はアプリケーションをangularjs)JavaScriptでwww.example-b.com (laravel + laraver-CORS + laravel-Excelとサーバー)

最も参考になるコメント

PHPExcelが実際のダウンロードを処理するため、このパッケージはResponseの使用をサポートしていません。 ヘッダーを自分で渡す必要があります(2番目のパラメーター)

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

全てのコメント3件

PHPExcelが実際のダウンロードを処理するため、このパッケージはResponseの使用をサポートしていません。 ヘッダーを自分で渡す必要があります(2番目のパラメーター)

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

これをドキュメントに追加してください。

こんにちは、私は実際にLumenプロジェクトでLaravel-Excelを使用しており、Excelファイルでデータをエクスポートして以下のようにダウンロードしようとしています。

My UsersExport

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');
    } 

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

そして、Excel()関数を実行しようとすると、このエラーが発生します

image

ただし、私はすでにCorsMiddleware.phpを作成し、それをbootstrap / app.phpに追加しており、他のすべての関数はそのおかげで非常にうまく機能します。

app.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 評価