我已经成功地将图像文件以二进制格式存储在 mongdb 中。但是当我从 mongodb 获取图像时,我得到的是相同的二进制格式。但是我需要这个图像文件。请有人帮忙
这是我使用的代码
def retrieve(request):
db=pymongo.connection.Connection('localhost',27017).demo1
grid=gridfs.GridFS(db)
output=grid.get_last_version(filename='shiva.jpg')
return HttpResponse(output)
最佳答案
您好,我已经使用 python 成功地从 mongodb 插入和检索图像..
def insert_image(request):
with open(request.GET["image_name"], "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
print encoded_string
abc=db.database_name.insert({"image":encoded_string})
return HttpResponse("inserted")
def retrieve_image(request):
data = db.database_name.find()
data1 = json.loads(dumps(data))
img = data1[0]
img1 = img['image']
decode=img1.decode()
img_tag = '<img alt="sample" src="data:image/png;base64,{0}">'.format(decode)
return HttpResponse(img_tag)
关于python - 如何从mongodb检索图像文件到html页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20718251/