【问题标题】:laravel response stream download returns empty filelaravel 响应流下载返回空文件
【发布时间】:2020-03-18 06:30:25
【问题描述】:

我有一个控制器方法,它接收用户的下载请求,使用请求的正文参数调用 REST API 端点。

API 作为回报,发送一个文件。

我正在使用 Guzzle 发出 API 请求和 laravel 版本 5.4

$response->getBody()->getContents();的内容为:

"""
request_id\ts_sku\tr_product_url\thuman_verdict\n
199\tSG-948\thttps://www.amazon.com/gp/offer-listing/B076CP7J4P?condition=new\tMatch\n
199\tZIB-601\thttps://www.amazon.com/gp/offer-listing/B07D9PN39C?condition=new\tMatch\n
199\tRCM-LG526\thttps://www.amazon.com/gp/offer-listing/B07FDJ3W7P?condition=new\tMatch\n
199\tGSI-335\thttps://www.amazon.com/gp/offer-listing/B07GNX1VSG?condition=new\tMatch\n
199\tZIB-489\thttps://www.amazon.com/gp/offer-listing/B07D9RCGXM?condition=new\tMatch\n
"""

从 API 接收到的标头响应也是:

Content-Type : ('text/tab-separated-values', none)
Content-Disposition : attachment; filename=/path/to/file1.tsv

我正在尝试在我的控制器中接收此文件并将响应作为流下载返回到用户的浏览器。

这里是控制器方法:

    $client = new Client(["base_uri" => $this->apiBaseUrl]);
    $url = 'download/filterdownload/';

    try {
        $response = $client->request('POST', $url, ['headers'=>['Content-Type' => 'application/json'], 'body'=>json_encode($filterArr)]);
        # $resArr = json_decode($response, true);
    } catch (\Exception $e) {
        return redirect()->back()->with('error', $e->getMessage());
    }

    if(!empty($resArr) && array_key_exists('response', $resArr))
        return redirect()->back()->with('error', $resArr['response']);

    # return response
    $file = $response->getBody()->getContents();

    $file_name = $filterArr['client_name'].'_'.Carbon::now()->toDateString().'.tsv';
    $headers = [
        'Content-Disposition' => 'attachment; filename='. $file_name. ';'
    ];
    return response()->stream(function () {
        $file;
    }, 200, $headers);

这会下载一个文件,但该文件是空的。

【问题讨论】:

    标签: php laravel-5 laravel-response


    【解决方案1】:

    也许有帮助:

     # return response
        $file = $response->getBody()->getContents();
    
        $file_name = $filterArr['client_name'].'_'.Carbon::now()->toDateString().'.tsv';
        $headers = [
            'Content-Disposition' => 'attachment; filename='. $file_name. ';'
        ];
        return response()->stream(function () use ($file)  { <-- edit
            $file;
        }, 200, $headers);
    

    【讨论】:

    • 我认为这不是正确的方法,因为您正在尝试对文件内容使用流式响应。为了做到这一点,您需要在响应中使用类似 readfile($file_full_path) 的东西,如下所示: return response()->stream(function () use ($full_file_path) { readfile($full_file_path); }, 200 , $headers); 现在它必须工作,我告诉你,因为我已经尝试过下载 .zip 存档的方法,但它没有工作,我刚刚提供的编辑 - 它工作正常.在此处阅读有关 readfile() 的更多信息 - php.net/manual/en/function.readfile.php
    • 这仍然为我返回一个空文件。
    【解决方案2】:

    试试这个

            $streamFileData = $response->getBody()->getContents();
            $fileName = "test.pdf";
            $headers = [
                'Content-Type'        => 'application/octet-stream',
                'Content-Disposition' => 'attachment; filename=' . $fileName,
            ];
            $response = response( $streamFileData, 200, $headers );
            return $response;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2021-09-19
      • 2020-01-22
      • 2019-01-04
      相关资源
      最近更新 更多