I try to download a file (Attachment here) from a Controller
Here is my code :
/**
* @param Request $request
* @param Attachment $attachment
*
* @Route("/message/{thread_id}/download/{attachment_id}", name="faq_download_attachment")
* @ParamConverter("attachment", options={"id" = "attachment_id"})
*
* @return BinaryFileResponse
*
* @throws \Symfony\Component\Filesystem\Exception\FileNotFoundException
*/
public function downloadFiletAction(Request $request, Attachment $attachment)
{
$mountManager = $this->get('oneup_flysystem.mount_manager');
$fileSystem = $mountManager->getFilesystem('faq');
return new BinaryFileResponse($fileSystem->getAdapter()->getPathPrefix() . $attachment->getFilename());
}
This code :
$fileSystem->getAdapter()->getPathPrefix() . $attachment->getFilename()
return an absolute path (Which is correct)
Finally, I get this error => The content cannot be set on a BinaryFileResponse instance.
It seems that when I instanciate a BinaryFileResponse, symfony puts it’s content to NULL (that what I want) but when I put the return statement. my content is replaced by a void string which is a bad thing because the following function of BinaryFileResponse is throwing an exception.
public function setContent($content)
{
if (null !== $content) {
throw new \LogicException('The content cannot be set on a BinaryFileResponse instance.');
}
}
Moreover, if I delete the if statement in the setContent Method Everything works like a charm. (but this is not the best thing to do)
Thank you for your help
I've tested your code and it worked for me. Try replacing the return statement by this:
return BinaryFileResponse::create($fileSystem->getAdapter()->getPathPrefix() . $attachment->getFilename());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With