-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.py
38 lines (35 loc) · 1.18 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from bottle import route, run, request, response
import json
@route('/', method='GET')
def do_form():
meta = { 'id':1, 'name':'alessandra' }
form = """
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Upload Test</title>
</head>
<body>
<form action="/upload" method="post"
enctype="multipart/form-data">
<textarea name=meta rows=10 cols=60>%s</textarea><br/>
<input type="file" name="data"/>
<input type="submit" value="POST data" />
</form>
</body>
</html>
""" % (json.dumps(meta),)
response.content_type = 'text/html; charset=utf-8'
return form
@route('/upload', method='POST')
def do_upload():
meta = request.forms.meta
data = request.files.get("data")
if data and data.file:
filename = "/tmp/%s" % data.filename
data.save(filename, overwrite=True, chunk_size=65536)
return "<b>Metadata:</b><br/>%s<hr/>You uploaded <b>%s</b>." \
% (meta, filename)
return "You missed a field."
run(host='0.0.0.0', port=8080, debug=True, reloader=True)