Last active
June 11, 2019 00:22
-
-
Save nazimamin/3bcaf92e8728f2bbd85e to your computer and use it in GitHub Desktop.
File uploading using flask
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
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg']) | |
APP_ROOT = os.path.dirname(os.path.abspath(__file__)) | |
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads') | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
# example taken from http://flask.pocoo.org/docs/0.10/patterns/fileuploads/ | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS | |
@app.route('/upload', methods=['POST']) | |
def upload(): | |
file = request.files['file'] | |
if file and allowed_file(file.filename): | |
filename = secure_filename(file.filename) | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
return "success" | |
@app.route('/uploads/<filename>', methods=['GET']) | |
def uploaded_file(filename): | |
return send_from_directory(app.config['UPLOAD_FOLDER'], | |
filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment