gpt4 book ai didi

symfony - 二进制文件响应 : The content cannot be set on a BinaryFileResponse instance

转载 作者:行者123 更新时间:2023-12-02 14:01:13 24 4
gpt4 key购买 nike

我尝试从 Controller 下载文件(此处为附件)

这是我的代码:

/**
* @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()

返回绝对路径(这是正确的)

enter image description here最后,我收到这个错误=>无法在 BinaryFileResponse 实例上设置内容。

似乎当我实例化 BinaryFileResponse 时,symfony 将其内容设置为 NULL (这就是我想要的),但是当我放置 return 语句时。我的内容被替换为空字符串,这是一件坏事,因为 BinaryFileResponse 的以下函数抛出异常。

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

此外,如果我删除 setContent 方法中的 if 语句,一切都会变得非常顺利。 (但这不是最好的做法)

感谢您的帮助

最佳答案

我已经测试了你的代码,它对我有用。尝试用以下内容替换 return 语句:

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

关于symfony - 二进制文件响应 : The content cannot be set on a BinaryFileResponse instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com