Symfony Binary File Response fails at the end of the download process

1.5k views Asked by At

I usually don't post any question on Stack Overflow because I always find an answer to whatever problem I'm in trouble with. But here, I haven't found a clear answer so if you can help me, thanks a lot !

I'm working on a Symfony 3.4 website. I've made a script inside my controller to return a file as a BinaryFileResponse.

$response = new BinaryFileResponse($pathtofile);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
return $response;

At first, the downloading process works ok, but just at the end, an error occurs. Chrome returns a "Network error" failure and the downloaded file stays as a .crdownload instead of being rename as an .mp3 in my case. (Note : If I rename manually the file as an .mp3, it plays correctly.)

I've searched for a possible issue on the content-length property of the headers, but it seems to be sent correctly when I check the Symfony Profiler Capture here

I've found a way to bypass this error by using a stream as mentionned in the Symfony Docs, like this :

$stream  = new Stream('path/to/stream');
$response = new BinaryFileResponse($stream);

My file is served by a PHP script, but it is not generated by it. Is there a way to serve it as a static file instead of a stream ?

With the stream solution, the client can't see the actual size of the file, which is not really user friendly. Also, it is really slow.

Thanks for your help !

EDIT : the first solution works on localhost, but not on prod server.

0

There are 0 answers