VichUploaderBundle icon indicating copy to clipboard operation
VichUploaderBundle copied to clipboard

Allow BinaryFileResponse in DownloadHandler?

Open benoliver999 opened this issue 6 years ago • 2 comments

One thing that might be handy would be the option to return a simple BinaryFileResponse with the DownloadHandler.

My reasoning is that even though you want to serve files through the controller (to check access rights in my case and keep stuff out of web/), you don't always strictly want a download.

I have a few docs (that were not uploaded using vich) that I serve up using BinaryFileResponse, and people like it because it doesn't clutter their downloads.

I am just gauging interest here, feel free to shoot me down ;)

benoliver999 avatar Aug 09 '17 12:08 benoliver999

What would be the difference, compared with the currently implemented StreamedResponse?

garak avatar Aug 11 '17 09:08 garak

It comes down to Content-Disposition and how it is handled with a streamed response.

What I'm looking for is DISPOSITION_INLINE to be set (optionally) so that browsers can open files when possible.

However, with the StreamedResponse I cannot get this to work even when it's set to INLINE.

My test-case was simply to change ATTACHMENT to INLINE in DownloadHandler so if I'm doing something wrong let me know.

With a BinaryFileResponse you get DISPOSITION_INLINE by default but you also get to choose. I have tested the following (from the docs) on another project of mine and it seems to work in Chrome and Firefox.

$response = new BinaryFileResponse($file);
$response->headers->set('Content-Type', 'text/plain');
$response->setContentDisposition(
    ResponseHeaderBag::DISPOSITION_INLINE,
    'filename.txt'
);
return $response;

With a PDF for instance, the file opens in the built-in browser PDF reader. Change INLINE to ATTACHMENT and the browser initiates a download.

It's only a suggestion from me, not a critical thing IMO. I figured it might be worth doing since:

  • The symfony docs recommend it for static files (which is surely what we are dealing with here?)
  • It would be nice to choose between a download and opening inline.

benoliver999 avatar Aug 11 '17 10:08 benoliver999