Created
March 4, 2013 01:20
-
-
Save jdstanhope/5079277 to your computer and use it in GitHub Desktop.
Google App Engine example of inline image uploading using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
application: inline-upload | |
version: 1 | |
runtime: python27 | |
api_version: 1 | |
threadsafe: yes | |
handlers: | |
- url: /favicon\.ico | |
static_files: favicon.ico | |
upload: favicon\.ico | |
- url: .* | |
script: main.app | |
libraries: | |
- name: webapp2 | |
version: "2.5.2" | |
- name: jinja2 | |
version: "2.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Inline Image Upload Test</title> | |
<script type="text/javascript"> | |
function startUpload() { | |
document.getElementById('upload_progress').style.visibility = 'visible'; | |
return true; | |
} | |
function showImage(id) { | |
var image = document.getElementById('upload_image'); | |
image.style.visibility = 'visible'; | |
image.src = '/image/' + id; | |
} | |
function stopUpload(success, imageId) { | |
var result = document.getElementById('upload_result'); | |
result.style.visibility = 'visible'; | |
if (success) { | |
showImage(imageId); | |
result.innerText = 'Upload Succeeded' | |
} | |
else { | |
result.innerText = 'Upload Failed' | |
} | |
document.getElementById('upload_progress').style.visibility = 'hidden'; | |
return true; | |
} | |
</script> | |
<style> | |
.hidden { | |
visibility: hidden; | |
} | |
</style> | |
</head> | |
<body> | |
{% if user %} | |
<h5>Welcome {{ user.nickname() }} <a href="{{ logout }}"> [Log out]</a></h5> | |
{% else %} | |
<h5><a href="{{ login }}">Log in</a></h5> | |
{% endif %} | |
<h1>Inline Image Upload Test</h1> | |
<form action="/upload" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();"> | |
<label for="file">Image</label> | |
<input type="file" name="file" id="file"/> | |
<input type="submit" name="action" value="Upload"/> | |
</form> | |
<div id="upload_progress" class="hidden">Uploading</div> | |
<div id="upload_result" class="hidden"></div> | |
<img id="upload_image" class="hidden"/> | |
<iframe id="upload_target" name="upload_target" src="#" style="width: 0 ;height: 0; border: 0 solid #fff"></iframe> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Copyright 2007 Google Inc. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
import httplib | |
import os | |
from google.appengine.api import users | |
from google.appengine.ext import db | |
import webapp2 | |
import jinja2 | |
root_dir = os.path.dirname(__file__) | |
search_paths = [os.path.join(root_dir, 'templates')] | |
env = jinja2.Environment(loader=jinja2.FileSystemLoader(search_paths)) | |
class Image(db.Model): | |
name = db.StringProperty(indexed=True) | |
data = db.BlobProperty() | |
class MainHandler(webapp2.RequestHandler): | |
def get(self): | |
template = env.get_template('main.html') | |
context = { | |
'user': users.get_current_user(), | |
'login': users.create_login_url('/'), | |
'logout': users.create_logout_url('/'), | |
} | |
content = template.render(context) | |
self.response.write(content) | |
class UploadHandler(webapp2.RequestHandler): | |
def post(self): | |
field_storage = self.request.POST['file'] | |
data = self.request.get('file') | |
template = env.get_template('upload.html') | |
if data is None: | |
content = template.render({'result': 'false'}) | |
self.response.write(content) | |
else: | |
image = Image(name=field_storage.filename, data=data) | |
image.put() | |
content = template.render({'result': 'true', 'image_id': image.key().id()}) | |
self.response.write(content) | |
class ImageHandler(webapp2.RequestHandler): | |
def get(self, image_id): | |
image = Image.get_by_id(int(image_id)) | |
self.response.write(image.data) | |
routes = [webapp2.Route(r'/', MainHandler, name='main'), | |
webapp2.Route(r'/upload', UploadHandler, name='upload'), | |
webapp2.Route(r'/image/<image_id>', ImageHandler, name='image')] | |
app = webapp2.WSGIApplication(routes, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script language="javascript" type="text/javascript"> | |
{% if result %} | |
window.top.window.stopUpload(true, {{ image_id }}); | |
{% else %} | |
window.top.window.stopUpload(false); | |
{% endif %} | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment