Created
February 23, 2018 16:11
-
-
Save rlcarrca/7eaef58579970d040e953026a5a8eab2 to your computer and use it in GitHub Desktop.
Upload a file to the server using bottle
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
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) |
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
<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