fs.get_last_version返回什么数据“结构”? [英] What data 'structure' does fs.get_last_version return?

查看:131
本文介绍了fs.get_last_version返回什么数据“结构”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 get_last_version 从数据库中获取图像时,实际上返回的是数组,即构成文件的所有块的合并二进制数据(

When I use get_last_version to get an image from the database, what is actually returned ie an array, the merged binary data of all the chunks that make up the file (as a string), or something else?

dbname = 'grid_files'
db = connection[dbname]
fs = gridfs.GridFS(db)
filename = "my_image.jpg"
my_image_file = fs.get_last_version(filename=filename)

我想对base64编码 my_image_file 进行编码:

I'm wanting to base64 encode my_image_file with:

import base64

encoded_img_file = base64.b64encode(my_image_file)
return encoded_img_file

但是我遇到500错误。

But I'm getting a 500 error.

在文档中使用 get_last_version 时,我无法收集到实际返回的内容:

I haven't been able to glean what is actually returned when using get_last_version from the docs:

http: //api.mongodb.org/python/current/api/gridfs/#gridfs.GridFS.get_last_version

更多研究:

我遵循了这篇文章的逻辑:

I followed the logic from this post:

http://blog.pythonisito.com/2012/05/gridfs-mongodb-filesystem.html

在运行服务器的Python外壳程序中,可以看到返回了 Binary()-因此我应该能够对它进行base64编码如上所示?

And in shell running Python on server could see that Binary() was returned - so should I be able to base64 encode this as demonstrated above?:

>>> import pymongo
>>> import gridfs
>>> import os
>>> hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']
>>> conn = pymongo.MongoClient(host=hostname)
>>> db = conn.grid_files
>>> fs = gridfs.GridFS(db)
>>> list(db.fs.chunks.find())

[{u'files_id': ObjectId('52db4d9e70914413718f2ec4'), u'_id': ObjectId('52db4d9e7
0914413718f2ec5'), u'data': Binary('lots of binary code', 0), u'n': 0}]


推荐答案

除非有更好的答案,否则这是我想出的。

Unless there is a better answer, this is what I've come up with.

get_last_version 返回 Binary()对象。

关于base64编码并返回它,这是我的方法:

In regards to base64 encoding it, and returning it, this is how I did it:

dbname = 'grid_files'
db = connection[dbname]
fs = gridfs.GridFS(db)
filename = "my_image.jpg"
my_image_file = fs.get_last_version(filename=filename)
encoded_img_file = base64.b64encode(my_image_file.read())
return encoded_img_file

然后在前端通过以下方式访问它:

Then accessed it on the front end with:

$("#my_img").attr("src", "data:image/png;base64," + data);

这篇关于fs.get_last_version返回什么数据“结构”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆