Upload image in Flask
Solution 1:
you need to define the upload folder
from the flask
documentation
import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
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 user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
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))
https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
So your code would be UPLOAD_FOLDER = '/path/to/static/images'
or something like that
Solution 2:
- your form's
enctype
should bemultipart/form-data
(otherwise it does not work):
<form method="post" enctype="multipart/form-data">
...
</form>
- You should specify upload folder (otherwise it does not work):
app.config['UPLOAD_FOLDER'] = '/path/to/the/uploads'
- You should specify a name for your file input (otherwise it does not work):
<input type="file" name="file1">
- you should manually save the file with file
save()
method:
path = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
file1.save(path)
- Simple full example:
import os
from flask import Flask, request
UPLOAD_FOLDER = './upload'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file1' not in request.files:
return 'there is no file1 in form!'
file1 = request.files['file1']
path = os.path.join(app.config['UPLOAD_FOLDER'], file1.filename)
file1.save(path)
return path
return 'ok'
return '''
<h1>Upload new File</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<input type="submit">
</form>
'''
if __name__ == '__main__':
app.run()
- Detailed full example: https://flask.palletsprojects.com/en/1.1.x/patterns/fileuploads/
Solution 3:
Try to first store the image jpeg or jpg in a static folder
inside the static folder the image is stored
then in the html file
<img src="{{url_for('static', filename='Hermes.png')}}" align="middle" />
add this code line
In app.py the code
from flask import Flask, render_template, redirect, url_for, request
# Route for handling the login page logic
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('home.html')
@app.route('/register')
def register():
return render_template('register.html')
@app.route('/registerV')
def registerV():
return render_template('registerV.html')
In register.html the code
<html>
<head>
</head>
<body>
<div class="bd-example" align="middle">
<img src="{{url_for('static', filename='Hermes.png')}}" align="middle" />
</div>
</body>
</html>