Question

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)
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top