Search code examples
pythongoogle-app-engineblobblobstore

Display blob image in template Google App Engine (Python)


I am trying to display an uploaded blob image. It seems to be storing ok, but won't display on the html template when called. Even seems to be showing up in the logging. When looking at other examples, the only real difference I see is you have to iterate over a list, but really just want to pull only one picture. Any advice you can give is greatly appreciated. -Thanks

Handlers:

class PictureHandler(BaseHandler2):
def get(self, **kwargs):
    user_session = self.user
    user_session_object = self.auth.store.get_session(self.request)

    user_info = models.User.get_by_id(long( self.user_id ))
    user_info_object = self.auth.store.user_model.get_by_auth_token(
        user_session['user_id'], user_session['token']) 

    user = self.session.get('user') 

    uploads = db.GqlQuery("SELECT * FROM UserPictureUpload WHERE user =:1 ORDER BY created DESC", user_info.username).get()

    upload_url = blobstore.create_upload_url('/upload')

    params = {
    'upload_url': upload_url,
    'user': user_info.username,
    'uploads': uploads      
    }       

    return self.render_template('picture.html', **params)


class UploadHandler(blobstore_handlers.BlobstoreUploadHandler, BaseHandler2):
def post(self):
    user_session = self.user
    user_session_object = self.auth.store.get_session(self.request)

    user_info = models.User.get_by_id(long( self.user_id ))
    user_info_object = self.auth.store.user_model.get_by_auth_token(
        user_session['user_id'], user_session['token']) 

    user = self.session.get('user') 

    title = self.request.get('title')
    pic = self.request.get("picfile")   


    if pic:
        picture = db.Blob(pic)

    upload = UserPictureUpload(title = title, picture=picture, user=user_info.username)         
    upload.put()
    self.redirect('/settings/picture')  


class ViewHandler(blobstore_handlers.BlobstoreDownloadHandler, BaseHandler2):
def get(self):    
    user_session = self.user
    user_session_object = self.auth.store.get_session(self.request)

    user_info = models.User.get_by_id(long( self.user_id ))
    user_info_object = self.auth.store.user_model.get_by_auth_token(
        user_session['user_id'], user_session['token']) 

    user = self.session.get('user') 


    upload_key_str = self.request.params.get('key')

    if upload_key_str:              

        upload = db.get(upload_key_str)
        if upload:
            logging.info('** Upload Found** --  %s' % upload.picture)  

        self.send_blob(upload.picture)      


    if not upload_key_str:

        self.error(404)
        return 

Routing:

RedirectRoute('/settings/picture', handlers.PictureHandler, name='picture', strict_slash=True), 
RedirectRoute('/upload', handlers.UploadHandler, name='upload', strict_slash=True), 
RedirectRoute('/view', handlers.ViewHandler, name='view', strict_slash=True)

Html:

<form action="{{ upload_url }}"  method="post" enctype="multipart/form-data">
<label for="title">Title:</label>
<input type="text" id="title" name="title" /><br />
<label for="upload">File:</label>
<input type="file" id="upload" name="picfile" /><br />
<input type="submit" value="Upload Picture" />
</form>
<br />
<img src='/view?key={{uploads.key()}}' alt='no image'/></img>

App Engine Log:

INFO 2013-07-30 16:11:20,946 handlers.py:875] ** Upload Found** -- Content-Type: image/jpeg

Content-Length: 775702

Content-MD5: NWE0NGM3YmE1YmJlNGVjODY3MjMzZDY3ZTQ4MDY4NDg=

content-disposition: form-data; name="picfile"; filename="Jellyfish.jpg"

X-AppEngine-Upload-Creation: 2013-07-29 15:41:00.481000

INFO 2013-07-30 11:11:21,005 server.py:584] default: "GET /view?key=ag5kZXZ-c2FuZGVuZ2luZXIeCxIRVXNlclBpY3R1cmVVcGxvYWQYgICAgJjxtgsM HTTP/1.1" 200 -


Solution

  • It looks like you are uploading the blob in blobstore but creating a key from your post upload handler and storing it as another blob on picture property instead of getting the blobkey from the uploaded blobinfo which from here: https://developers.google.com/appengine/docs/python/tools/webapp/blobstorehandlers

    you can get with

    upload = self.get_uploads()[0]
    picture=upload.key()
    

    then that's what you put on your send_blob.