BinaryFileResponse: Content cannot be set in an instance of BinaryFileResponse

I am trying to download a file (Attachment here) from 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() 

returns the absolute path (which is correct)

enter image description here Finally, I get this error => Content cannot be set in an instance of BinaryFileResponse.

It looks like when I create a BinaryFileResponse, symfony puts its contents in NULL (this is what I want), but when I put the return statement. my content is replaced with an empty string, which is bad because the next BinaryFileResponse function throws an exception.

 public function setContent($content) { if (null !== $content) { throw new \LogicException('The content cannot be set on a BinaryFileResponse instance.'); } } 

Also, if I delete the if statement in the setContent method, everything works like a charm. (but this is not the best thing)

thanks for the help

+5
source share
1 answer

I checked your code and it worked for me. Try replacing the return statement with the following:

 return BinaryFileResponse::create($fileSystem->getAdapter()->getPathPrefix() . $attachment->getFilename()); 
+3
source

Source: https://habr.com/ru/post/1212194/


All Articles