giggsey / psr7-stream-response
从 PSR-7 流构建文件响应
1.1.0
2024-04-11 07:45 UTC
Requires
- php: >=8.0
- psr/http-message: ^1.0|^2.0
- symfony/http-foundation: ^7.0
README
为什么?
Symfony 的 BinaryFileResponse 允许向 HTTP 客户端展示文件以便下载。然而,它期望完整的文件路径。一些项目可能希望将 PSR-7 流直接流式传输到客户端。
如何使用
而不是返回 BinaryFileResponse,创建一个 PSR7StreamResponse,并返回它。
之前
$response = new BinaryFileResponse($filePath); $response = $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'my-file.mp3'); return $response;
之后
$response = new PSR7StreamResponse($stream, 'audio/mpeg'); $response = $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'my-file.mp3'); return $response;