我已经在Mongo DB GridFS中存储了一些图像。我现在正试图展示这张照片,但遇到了严峻的挑战。我看到的只是<Mongo Binary Data>
作为输出。我曾经尝试过头球攻门,但似乎没有什么好的。我看到的只是<Mongo Binary Data>
。我尝试发送一个图像标题(jpeg/png等),但图像显示为空。为什么?
这是我显示图像的代码:
public function someAction($imageID)
{
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$image = $dm->createQueryBuilder('Mybundle:Asset')
->field('id')->equals($imageID)
->getQuery()
->getSingleResult();
return new Response($image->getFile()->getBytes(), 200, array('Content-Type' => 'image/jpeg'));
}
当我尝试将内容类型更改为文本时,我会再次得到<Mongo Binary Data>
。
这是我的路由文件:
my_route:
pattern: /showimage/{imageID}
defaults: { _controller: MyBundle:someController:someAction}
requirements:
_method: GET
"<Mongo Binary Data>"
是从MongoBinData::__toString()返回的字符串。这种行为可以追溯到几年前,但我认为实现它是为了避免在MongoBinData被转换为字符串时无意中生成大输出或回显不可打印的字符。
在您的情况下,我假设$image->getFile()
对应于Doctrine'MongoDB'GridFSFile
对象。我会在那里开始调试,看看getBytes()
是否返回对象的内部$bytes
属性、一些文件内容或链接到MongoGridFSFile::getBytes()。
此外,共享Asset类的模型/映射信息以及同一查询返回的未水合数据也会有所帮助。将->hydrate(false)
添加到查询构建器链将实现后者。