mongo gridfs 与 python
我想直接从 python 脚本提供来自 gridfs 的图像,但我看到的只是空白屏幕:
#!/usr/bin/env python
from pymongo import Connection
import gridfs
db = Connection().gridfs_example
fs = gridfs.GridFS(db)
f= fs.get_last_version('myimage')
print "Content-type: %s \n\n " % f.content_type
print "Content-Length: %d \n\n" % f.length
print f.read()
如果我将 f.read() 的输出写入文件,我能够看到“有效”图像 我能够从本地 FS 显示此图像:
#!/usr/bin/env python
img = open('image.jpg','rb').read()
print "Content-type: image/jpeg"
print "Content-Length: %d\n" % len(img)
print img
我做错了什么?
I want to serve image from gridfs directly from python script but only what I see is blank screen:
#!/usr/bin/env python
from pymongo import Connection
import gridfs
db = Connection().gridfs_example
fs = gridfs.GridFS(db)
f= fs.get_last_version('myimage')
print "Content-type: %s \n\n " % f.content_type
print "Content-Length: %d \n\n" % f.length
print f.read()
if I write output from f.read() to file I able to see "valid" image
I able to show this image from local FS with:
#!/usr/bin/env python
img = open('image.jpg','rb').read()
print "Content-type: image/jpeg"
print "Content-Length: %d\n" % len(img)
print img
what I did wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是有问题的线。 GridFS 构造函数采用数据库实例,而不是连接对象。所以你必须做类似的事情:
希望这有帮助!
is the offending line. The GridFS constructor takes an instance of Database, not a connection object. So you would have to do something like:
Hope this helps!
如果我们可以看到您的脚本的一些原始输出,将会有所帮助。
要检查的一件事是响应标头是否正确终止。 print 语句在输出末尾添加一个换行符,这意味着您可以从内容类型标头字符串中删除第二个“\n”。
It would help if we could see some raw output from your script.
One thing to check is that the response headers are correctly terminated. The print statement adds a newline character to the end of the output, which means you can remove the second '\n' from the content-type header string.