Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryFileResponse : The content cannot be set on a BinaryFileResponse instance

Tags:

symfony

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)

enter image description here 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

like image 847
Jaybe Avatar asked Jan 29 '15 13:01

Jaybe


1 Answers

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());
like image 50
Javier C. H. Avatar answered Dec 20 '22 13:12

Javier C. H.