Last active
April 11, 2018 21:07
-
-
Save nurely/68cb7cf6bfc2da881004d070c536f258 to your computer and use it in GitHub Desktop.
Simple File Upload - Make Sure to have a folder named upload in the same directory
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
import os | |
from flask import Flask, request, redirect, url_for | |
from werkzeug import secure_filename | |
PROJECT_HOME = os.path.dirname(os.path.realpath(__file__)) | |
UPLOAD_FOLDER = '{}/uploads/'.format(PROJECT_HOME) | |
ALLOWED_EXTENSIONS = set(['txt','pdf']) | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS | |
@app.route("/", methods=['GET', 'POST']) | |
def index(): | |
if request.method == 'POST': | |
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 redirect(url_for('index')) | |
return """ | |
<!doctype html> | |
<title>Upload new File</title> | |
<h1>Upload new File</h1> | |
<form action="" method=post enctype=multipart/form-data> | |
<p><input type=file name=file> | |
<input type=submit value=Upload> | |
</form> | |
<p>%s</p> | |
""" % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],)) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=5001, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment