如何从mongodb检索图像文件到html页面 [英] How to retrieve image files from mongodb to html page

查看:58
本文介绍了如何从mongodb检索图像文件到html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功以二进制格式将图像文件存储在mongdb中.但是当我从mongodb获取图像时,我也获得了相同的banary格式.但是我需要此图像文件.请有人帮忙

I have successfully stored image files in mongdb in binary format.but when i m getting image from mongodb i m getting the same banary format.But i need this image file.Please someone could help

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插入并检索了图像.

Hi i have successfully inserted and retrieved image from mongodb with python..

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)

这篇关于如何从mongodb检索图像文件到html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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