Created
September 11, 2015 00:36
-
-
Save coopernurse/aaabb3d93b0cd818ba7b to your computer and use it in GitHub Desktop.
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 | |
import time | |
from bottle import route, run, template, get, post, request, static_file | |
@route('/pics/<filepath:path>') | |
def server_static(filepath): | |
return static_file(filepath, root='pics') | |
@route('/hello/<name>') | |
def index(name): | |
return template('<b>Hello {{name}}</b>!', name=name) | |
@get('/echo') | |
def get_echo(): | |
return template('echo') | |
@post('/echo') | |
def post_echo(): | |
params = { | |
"name" : request.forms.get('name'), | |
"food" : request.forms.get('food') | |
} | |
return template('echo_post', params) | |
@get('/') | |
def form(): | |
return template('upload') | |
@post('/') | |
def form_submit(): | |
pic = request.files.get('pic') | |
ext = pic.filename.split(".")[-1] | |
fname = "pics/%d.%s" % (int(time.time()*1000), ext) | |
params = { | |
"name" : request.forms.get('name'), | |
"pic" : pic.filename, | |
"pic_local" : fname | |
} | |
pic.save(fname) | |
return template('upload_done', params) | |
run(host='localhost', port=8080, debug=True, reloader=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
<html> | |
<head> | |
<title>Upload!!!!!</title> | |
<script src="http://cdn.jsdelivr.net/mithril/0.1.22/mithril.min.js"></script> | |
<script> | |
function uploadStuff(files) { | |
var formData = new FormData; | |
formData.append("pic", document.getElementById("pic").files[0]); | |
return m.request({ | |
method: "POST", | |
url: "/", | |
data: formData, | |
serialize: function(value) {return value} | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<h1>Upload!</h1> | |
<input type="file" name="pic" id="pic" /> | |
<a href="#" onclick="uploadStuff(); return false;">Upload!!</a> | |
<!-- | |
<form method="post" action="#" enctype="multipart/form-data"> | |
<table> | |
<tr> | |
<td>Name:</td> | |
<td><input type="text" name="name" size="30" /></td> | |
</tr> | |
<tr> | |
<td>Pic:</td> | |
<td><input type="file" name="pic" id="pic" /></td> | |
</tr> | |
<tr> | |
<td> </td> | |
<td><input type="submit" value="Let's do this!" onclick="uploadStuff(); return false;" /></td> | |
</tr> | |
</table> | |
</form> | |
--> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment