1def allowed_file(filename):
2 return '.' in filename and \
3 filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
4
5@app.route('/', methods=['GET', 'POST'])
6def upload_file():
7 if request.method == 'POST':
8 # check if the post request has the file part
9 if 'file' not in request.files:
10 flash('No file part')
11 return redirect(request.url)
12 file = request.files['file']
13 # if user does not select file, browser also
14 # submit an empty part without filename
15 if file.filename == '':
16 flash('No selected file')
17 return redirect(request.url)
18 if file and allowed_file(file.filename):
19 filename = secure_filename(file.filename)
20 file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
21 return redirect(url_for('uploaded_file',
22 filename=filename))
23 return '''
24 <!doctype html>
25 <title>Upload new File</title>
26 <h1>Upload new File</h1>
27 <form method=post enctype=multipart/form-data>
28 <input type=file name=file>
29 <input type=submit value=Upload>
30 </form>
31 '''
32
1import os
2from flask import Flask, flash, request, redirect, url_for
3from werkzeug.utils import secure_filename
4
5UPLOAD_FOLDER = '/path/to/the/uploads'
6ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
7
8app = Flask(__name__)
9app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
10