Ответ файла Laravel/Lumen

Мне нужно для потоковой передачи содержимого файлов (таких как изображения и другие типы 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,

Любые предложения очень приветствуются.

2 ответа

Решение

В Lumen вы можете использовать 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;

Вы можете найти документацию здесь.

В 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);
    }
}
Другие вопросы по тегам