I need to use BinaryFileResponse for correct handling of videos with Length Headers and co. Also I want the user to allow configured other storages (S3, Dropbox). The flysystem readStream method will return a resource but BinaryFileResponse needs a string path. Whats the best way to handle this? First download the whole file to the server until response?
Asked
Active
Viewed 2,515 times
6
Alexander Schranz
- 2,154
- 2
- 25
- 42
1 Answers
12
You can use the StreamedResponse class for this. You'll have to do some data wiring yourself but it's pretty straight forward. For example, this code was used to enable "forced downloads" for PDF's.
return new StreamedResponse(function () use ($stream) {
fpassthru($stream);
exit();
}, 200, [
'Content-Transfer-Encoding', 'binary',
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s.pdf"', $filename),
'Content-Length' => fstat($stream)['size'],
]);
Frank de Jonge
- 1,607
- 1
- 15
- 23
-
1Think the `exit()` can be removed for compatibility with swoole, roadrunner or other long running php servers. – Alexander Schranz Apr 06 '22 at 15:57