Uploading images - Google App Engine + Python
Question: I'm using this link as an example to uploading images:

<https://gist.github.com/jdstanhope/5079277>

My HTML code:

    
    
     <form action="/upload_image" method="post" id="form1" runat="server">
           <div class="fileButtons">
             <input type='file' id="imgInp" name="imgInput" accept="image/*"/><br><br>
             <input type='button' id='remove' value='Remove' />
            </div></form>
    

main.py:

    
    
    class SetImage(webapp2.RequestHandler):
        def post(self):
            logging.debug("test")
            id = str(self.request.get('id'))
            image = self.request.get('imgInput')
    
    app = webapp2.WSGIApplication([('/upload_image', SetImage),
                                   ('/', MainPage)], debug=True)
    

But when I add an image, nothing is being done, and the log console doesn't
print:

    
    
    logging.debug("test")
    


Answer: The recommended way of uploading images to GAE is by using
[blobstore](https://developers.google.com/appengine/docs/python/blobstore/).

Here is a quick breakdown of the doc to help you achieve this fast:

Imports:

    
    
    import os
    import urllib
    import webapp2
    
    from google.appengine.ext import blobstore
    from google.appengine.ext.webapp import blobstore_handlers
    

Serve the form HTML. this form performs the POST to GAE with the selected
files data:

    
    
    class MainHandler(webapp2.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>""")
    

Add a handler for receiving the POST data (binary content of the file). the
last line in this function is to redirect the response to the location from
where the file could be downloaded:

    
    
    class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
        def post(self):
            # 'file' is file upload field in the form
            upload_files = self.get_uploads('file')
            blob_info = upload_files[0]
            self.redirect('/serve/%s' % blob_info.key())
    

Add handler to serve the image that you uploaded using the UploadHandler
described above:

    
    
    class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self, resource):
            resource = str(urllib.unquote(resource))
            blob_info = blobstore.BlobInfo.get(resource)
            self.send_blob(blob_info)
    

And finally, define the routes for the app:

    
    
    app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/upload', UploadHandler),
                               ('/serve/([^/]+)?', ServeHandler)],
                                debug=True)
    

