Skip to content

Instantly share code, notes, and snippets.

@rlcarrca
Created February 23, 2018 16:11
Show Gist options
  • Save rlcarrca/7eaef58579970d040e953026a5a8eab2 to your computer and use it in GitHub Desktop.
Save rlcarrca/7eaef58579970d040e953026a5a8eab2 to your computer and use it in GitHub Desktop.
Upload a file to the server using bottle
from __future__ import unicode_literals
import os
from bottle import route, request, static_file, run, response
@route('/')
def root():
return static_file('test.html', root='.')
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.png', '.jpg', '.jpeg', '.xlsx'):
return "File extension not allowed."
save_path = "/tmp/{category}".format(category=category)
if not os.path.exists(save_path):
os.makedirs(save_path)
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
return "File successfully saved to '{0}'.".format(save_path)
if __name__ == '__main__':
run(host='localhost', port=8080)
<form action="/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category" />
Select a file: <input type="file" name="upload" />
<input type="submit" value="Start upload" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment