display image attach from couchDB in html via flask/python

后端 未结 2 1491
梦毁少年i
梦毁少年i 2021-01-17 04:55

I\'m tying to get an image attachment from couchdb using flask & python then pass the image to imgurl.html to be displayed. The problem is that I\'m only get this:

相关标签:
2条回答
  • 2021-01-17 05:34

    Before I saw @SHC's answer I managed to come up with a solution my self. What I did was retrieve the name of the attachment and then append that to the end of a url like i.e "http:localhost:5984/dbName/docId/attachmentName". I passed that url to the html img src and it worked then. Thank you you @SHC for your answer. sorry I didnt see it sooner.

    0 讨论(0)
  • 2021-01-17 05:42

    One option could be to base64 encode the image data returned from couchdb and pass the encoded data as a string to the template where you can render it. E.g.

    img = db.get_attachment('2', 'aboutme.jpg', default=None).read()
    img_b64 = base64.b64encode(img)
    
    def imgurl():
       return render_template('imgurl.html', doc = img_b64)
    

    Then in the template:

    <img src="data:image/png;base64,{{ doc }}" id="imgslot" >
    

    Another option depending on your security model could be to serve the image directly from couchdb by adding the image url to the image tag, e.g. Getting url for an attachment

    0 讨论(0)
提交回复
热议问题