How to display picture from memory in Django?

只愿长相守 提交于 2021-02-05 11:23:08

问题


I know how to show picture as a page from memory just using like this:

import cStringIO

mStream = cStringIO.StringIO(picBin)

return HttpResponse(mStream.getvalue(),"image/jpg")

But what if I don't want to show the picture as a page, instead I want to show it within a page, say using in HTML, does someone has an idea what I should set the "src" if the picture is loaded from memory?


回答1:


You'll need to base-64 encode the image contents into a Data URI:

data_uri = 'data:image/jpg;base64,'
data_uri += mStream.getvalue().encode('base64').replace('\n', '')

Now you can shove data_uri into the src attribute of an image.



来源:https://stackoverflow.com/questions/12487698/how-to-display-picture-from-memory-in-django

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