【发布时间】:2015-01-29 13:49:54
【问题描述】:
我尝试从控制器下载文件(此处为附件)
这是我的代码:
/**
* @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());
}
这段代码:
$fileSystem->getAdapter()->getPathPrefix() . $attachment->getFilename()
返回绝对路径(正确)
最后,我得到这个错误=> 无法在 BinaryFileResponse 实例上设置内容。
似乎当我实例化一个 BinaryFileResponse 时,symfony 将它的内容设置为 NULL(我想要的)但是当我放置 return 语句时。我的内容被替换为 void 字符串,这是一件坏事,因为 BinaryFileResponse 的以下函数正在引发异常。
public function setContent($content)
{
if (null !== $content) {
throw new \LogicException('The content cannot be set on a BinaryFileResponse instance.');
}
}
此外,如果我删除 setContent 方法中的 if 语句,一切都会像魅力一样工作。 (但这不是最好的做法)
感谢您的帮助
【问题讨论】:
标签: symfony