RFF
RFF

Reputation: 271

Getting a Blobstore key

I am reading about the Blobstore in Google App Engine. The code below is from the sample documentation. After the user selects a file to upload and clicks Submit, how do I get the key into a javascript variable? I can show it on a page, but I only want to keep it for later use. Obviously, I am new to Web programming.

#!/usr/bin/env python
#

import os
import urllib
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
  def get(self):
    upload_url = blobstore.create_upload_url('/upload')
    self.response.out.write('<html><body>')
    self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
    self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit"
    name="submit" value="Submit"> </form></body></html>""")

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('file')  # 'file' is file upload     field in the form
    blob_info = upload_files[0]
    self.response.out.write('<html><body>')
    self.response.out.write(str(blob_info.key()))
    self.response.out.write('</body><html>')

def main():
  application = webapp.WSGIApplication(
    [('/', MainHandler),
     ('/upload', UploadHandler),
    ], debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

Upvotes: 2

Views: 350

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

You could do something like this:

self.response.out.write("""
<html>
<script>
var blobKey = "%s";
</script>
<body>
...
</body>
</html>""" % (blob_info.key(),)

Upvotes: 6

Related Questions