Laravel/Lumen file response

2019-05-25 11:07发布

问题:

I need to stream file content (such as images and other mime types) from a Lumen resource server to a Laravel client server. I know in Laravel I can use:

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

However, the file method is absent in Laravel\Lumen\Http\ResponseFactory.

Any suggestions are very welcome.

回答1:

In Lumen you can use Symfony's 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;

You can find the documentation here.