How to retrieve image files from mongodb to html page

我的未来我决定 提交于 2021-02-19 02:27:07

问题


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

This is the code i used

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)

回答1:


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)


来源:https://stackoverflow.com/questions/20718251/how-to-retrieve-image-files-from-mongodb-to-html-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!