The instructions from Google about dynamically serving images with GAE are here: https://developers.google.com/appengine/articles/python/serving_dynamic_images
The only problem is that this method does not use AJAX, and I don’t think it is as efficient as I would expect.
I am trying to use AJAX with GAE Python 2.7 to serve multiple images dynamically, but I am focusing on serving one right now.
Python Server Side Code:
class GetImage(webapp2.RequestHandler):
def get(self):
problem = Problem.all()[0]
if(problem and problem.prompt_image):
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(problem.prompt_image)
Client Side AJAX Code:
$.ajax({
url: "/img",
type: "POST",
dataType: "html",
success: function(msg){
$("#right").append("<img>"+msg+"</img>");
}
});
The problem that I am facing is that I don’t know how to serve the blobstore contents via AJAX to the DOM. Can anyone point me in the right direction?
Thanks!
Edit: voscausa’s solution seems to be on the right track. However, it requires a blob_key. My model is as follows:
class Problem(db.Model):
prompt_image = db.BlobProperty()
I can’t seem to figure out how to get the Blob Key from the db.BlobProperty. Is this possible?
This problem gave me a real headache. Thanks to everyone, especially voscausa who put me on the right path to getting the solution. There is a lot of old material out there, this is the solution I used:
https://developers.google.com/appengine/docs/python/blobstore/overview#Uploading_a_Blob
(see complete sample application)