article

Monday, February 10, 2020

Uploading Files with Python Flask-Uploads


Uploading Files with Python Flask-Uploads

pip install flask-uploads


from flask_uploads import UploadSet, configure_uploads
#app.py
from flask import Flask, request, render_template, url_for
from flask_uploads import UploadSet, configure_uploads, IMAGES

app = Flask(__name__)

photos = UploadSet('photos', IMAGES)

app.config['UPLOADED_PHOTOS_DEST'] = 'static/img'
configure_uploads(app, photos)

@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        filename = photos.save(request.files['photo'])
        return filename
    return render_template('upload.html')
	
if __name__ == '__main__':
	app.run(debug=True)
//upload.html
<html>
<head>
	<title>Upload</title>
</head>
<body>
<h1>Uploading Files with Python Flask-Uploads</h1>
<form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}">
    <input type=file name=photo>
    <input type="submit">
</form>
</body>
</html>

Related Post