问题
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