Laravel/Lumen 文件响应

Laravel/Lumen file response

我需要将文件内容(例如图像和其他 MIME 类型)从 Lumen 资源服务器流式传输到 Laravel 客户端服务器。我知道 Laravel 我可以使用:

$headers = ['Content-Type' => 'image/png']; 
$path = storage_path('/mnt/somestorage/example.png')
return response()->file($path, $headers);

但是,file 方法在 Laravel\Lumen\Http\ResponseFactory 中不存在。

非常欢迎任何建议。

在 Lumen 中,您可以使用 Symfony 的 BinaryFileResponse

use Symfony\Component\HttpFoundation\BinaryFileResponse

$type = 'image/png';
$headers = ['Content-Type' => $type];
$path = '/path/to/you/your/file.png';

$response = new BinaryFileResponse($path, 200 , $headers);

return $response;

你可以find the documentation here.

Lumen中有一个功能: https://lumen.laravel.com/docs/8.x/responses#file-downloads

<?php

namespace App\Http\Controllers;

use App\Models\Attachment;
use Illuminate\Http\Request;

class AttachmentController extends AbstractController
{
    /**
     * Downloads related document by id
     */
    public function attachment(Request $request)
    {
        $path = "path/to/file.pdf";
        return response()->download($path);
    }
}