-
-
Save Internet-viewer/5757d901e70f83ccf4ccc53a75d80fe1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, flash, request, redirect, render_template | |
| from werkzeug.utils import secure_filename | |
| app=Flask(__name__) | |
| app.secret_key = "secret key" | |
| app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 | |
| path = os.getcwd() | |
| # file Upload | |
| UPLOAD_FOLDER = os.path.join(path, 'uploads') | |
| if not os.path.isdir(UPLOAD_FOLDER): | |
| os.mkdir(UPLOAD_FOLDER) | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) | |
| def allowed_file(filename): | |
| return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS | |
| @app.route('/') | |
| def upload_form(): | |
| return render_template('upload.html') | |
| @app.route('/', methods=['POST']) | |
| def upload_file(): | |
| if request.method == 'POST': | |
| # check if the post request has the file part | |
| if 'file' not in request.files: | |
| flash('No file part') | |
| return redirect(request.url) | |
| file = request.files['file'] | |
| if file.filename == '': | |
| flash('No file selected for uploading') | |
| return redirect(request.url) | |
| if file and allowed_file(file.filename): | |
| filename = secure_filename(file.filename) | |
| file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
| flash('File successfully uploaded') | |
| return redirect('/') | |
| else: | |
| flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif') | |
| return redirect(request.url) | |
| if __name__ == "__main__": | |
| app.run(host = '127.0.0.1',port = 5000, debug = False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment