Python 2.7 什么数据';结构';fs.get\u最后一个版本是否返回?
当我使用Python 2.7 什么数据';结构';fs.get\u最后一个版本是否返回?,python-2.7,pymongo,bottle,gridfs,Python 2.7,Pymongo,Bottle,Gridfs,当我使用get_last_version从数据库中获取图像时,实际返回的是什么,即数组、组成文件的所有块的合并二进制数据(作为字符串)或其他什么 dbname = 'grid_files' db = connection[dbname] fs = gridfs.GridFS(db) filename = "my_image.jpg" my_image_file = fs.get_last_version(filename=filename) 我想用以下代码对我的图像文件进行base64编码:
get_last_version
从数据库中获取图像时,实际返回的是什么,即数组、组成文件的所有块的合并二进制数据(作为字符串)或其他什么
dbname = 'grid_files'
db = connection[dbname]
fs = gridfs.GridFS(db)
filename = "my_image.jpg"
my_image_file = fs.get_last_version(filename=filename)
我想用以下代码对我的图像文件进行base64编码:
import base64
encoded_img_file = base64.b64encode(my_image_file)
return encoded_img_file
但我得到了一个500的错误
我无法从文档中收集使用get\u last\u version
时实际返回的内容:
更多研究:
我遵循了这篇文章的逻辑:
在服务器上运行Python的shell中,可以看到返回了Binary()
——那么我应该能够像上面演示的那样对其进行base64编码吗
>>> 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}]
除非有更好的答案,否则这就是我的想法
get\u last\u version
返回一个Binary()
对象
关于base64编码并返回它,我是这样做的:
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
然后在前端通过以下方式访问它:
$("#my_img").attr("src", "data:image/png;base64," + data);