- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试从 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()
返回绝对路径(这是正确的)
最后,我收到这个错误=>无法在 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/
当 Symfony2 Controller 使用 BinaryFileResponse 响应返回文件时,是否可以设置自定义文件名? 最佳答案 是的。 BinaryFileResponse 类有一个方法
我遇到了以下问题:我想在路线/getImage/{id} 上返回一个图像函数如下所示: public function getImage($id){ $image = Image::find($
我尝试从 Controller 下载文件(此处为附件) 这是我的代码: /** * @param Request $request * @param Attachment $attachme
在我的应用程序中,当用户访问返回 BinaryFileResponse 的页面时,他会断开连接。 当我在返回之前掷骰子时,我可以转储 session 和用户并重新加载而不会出现任何问题,但是每次应用程
我需要使用 BinaryFileResponse用于正确处理带有 Length Headers 和 co. 的视频。我还希望用户允许配置其他存储(S3、Dropbox)。飞行系统 readStream
我编写了一个中间件,以便用户登录后无法再次进入登录页面。如果已经登录,它将重定向到管理面板。 这是代码: class RevalidateBackHistory { public functi
我是一名优秀的程序员,十分优秀!