Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 什么数据';结构';fs.get\u最后一个版本是否返回?_Python 2.7_Pymongo_Bottle_Gridfs - Fatal编程技术网

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);