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
Top answer 1 of 6
59
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
2 of 6
16
- your form's
enctypeshould 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/
Medium
medium.com › @daiki139045 › how-to-create-image-uploading-app-in-python-using-flask-4dc77e1caa76
How to create image uploading app in Python using Flask | by Daiki | Medium
November 10, 2023 - if file.filename == '': flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) # Save the uploaded file as name of current time dt_now = dt.now().strftime("%Y%m%d%H%M%S%f") filename = dt_now + ".jpg" file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # PATH to static files img_dir = "./static/uploads/" path = img_dir + filename #Import picture date img = cv2.imread(path) # if img has date if len(img) != 0: #Convert to grayscale from uploaded picture gray = rgb_to_gray(img) #### Save the uploaded file
html - Python Flask App - Upload Image and Display - Stack Overflow
Just getting started with Python Flask App and working with HTML. I am trying to build a simple image upload page that will display the uploaded image. I have been able to successfully upload and ... More on stackoverflow.com
Flask Forms Uploading Files to Google Cloud Storage
After you save the file locally, use the google cloud storage library to upload the file (and then delete the file locally). You might be able to upload it directly from memory and skip saving it locally. https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-python More on reddit.com
To upload and display image in python-flask
Hi i understand your problem but i can't read this code would you mind editing this post And make it more readable 😅 More on reddit.com
how to open on a new tab or on a pop-up?
https://www.w3schools.com/tags/att_form_target.asp More on reddit.com
Videos
10:16
upload images and media in flask - YouTube
12:44
Python Flask Upload and Display Image | Flask tutorial - YouTube
10:00
Python Flask Upload multiple images and display multiple images ...
11:04
How to Upload Files with Flask Using Python - YouTube
20:41
Upload Profile Picture - Flask Fridays #38 - YouTube
29:03
13. Flask Upload Image - Easily Upload Image to Database - Python ...
Roy Tutorials
roytuts.com › home › flask › upload and display image using python flask
Upload and display image using Python Flask - Roy Tutorials
September 24, 2025 - Now you need template page for uploading file as well as it will also display the uploaded image. This is upload.html page kept under directory – templates, which is the standard directory for storing template or view file in flask application. ... <!doctype html> <title>Python Flask File Upload Example</title> <h2>Select a file to upload</h2> <p> {% with messages = get_flashed_messages() %} {% if messages %} <ul> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} </p> {% if filename %} <div> <img src="{{ url_for('display_image', filename=filename) }}"> </div> {% endif %} <form method="post" action="/" enctype="multipart/form-data"> <dl> <p> <input type="file" name="file" autocomplete="off" required> </p> </dl> <p> <input type="submit" value="Submit"> </p> </form>
Miguel Grinberg
blog.miguelgrinberg.com › post › handling-file-uploads-with-flask
Handling File Uploads With Flask - miguelgrinberg.com
If your application accepts uploads of a certain file type, it should ideally perform some form of content validation and reject any files that are of a different type. How you achieve content validation largely depends on the file types your application accepts. For the example application in this article I'm using images, so I can use the imghdr package from the Python standard library to validate that the header of the file is, in fact, an image.
Top answer 1 of 2
6
You are not matching your uploads directory to the view. Try this:
@app.route('/upload-image', methods=['GET', 'POST'])
def upload_image():
if request.method == "POST":
if request.files:
image = request.files["image"]
image.save(os.path.join(app.config["IMAGE_UPLOADS"], image.filename))
return render_template("upload_image.html", uploaded_image=image.filename)
return render_template("upload_image.html")
@app.route('/uploads/<filename>')
def send_uploaded_file(filename=''):
from flask import send_from_directory
return send_from_directory(app.config["IMAGE_UPLOADS"], filename)
In your template:
<img src="{{ url_for('send_uploaded_file', filename=uploaded_image) }}" />
2 of 2
1
In case you just want to upload, analyze, and display the image without saving it to storage, i.e. api and edged image analysis, this example might be a guide for you. Flaskimio
@app.route("/",methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'file' not in request.files:
print('No file attached in request')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
print('No file selected')
return redirect(request.url)
if file and check_allowed_file(file.filename):
filename = secure_filename(file.filename)
print(filename)
img = Image.open(file.stream)
with BytesIO() as buf:
img.save(buf, 'jpeg')
image_bytes = buf.getvalue()
encoded_string = base64.b64encode(image_bytes).decode()
return render_template('index.html', img_data=encoded_string), 200
else:
return render_template('index.html', img_data=""), 200
<form method="post" enctype="multipart/form-data">
<p><input type="file" id="file" name="file"><input type=submit value=Upload></p>
<img src="data:image/jpeg;base64,{{ img_data }}" id="img-upload" alt="img_data" class="img-upload"/>
</form>
Froala
froala.com › home › easily upload images to your server with python flask
Easily Upload Images To Your Server With Python Flask
December 23, 2025 - If you want complete editing and handling privileges, however, you should upload your images to a private server. To do this you need to use the Flask framework on the front end. We will look at how in a minute. Yes, it is possible to add an image to the HTML editor via an HTTP request. The server receives your request and returns the image link in JSON format while saving it to your browser. Once this process finishes, if you have the right hashmap, the editor will load the image. To get started using Python Flask, You’re going to need the image.min.js file and the Image plugin for the text editor.
Pythonise
pythonise.com › series › learning-flask › flask-uploading-files
Uploading files with Flask | Learning Flask Ep. 13 | pythonise.com
February 14, 2019 - from flask import request, redirect import os app.config["IMAGE_UPLOADS"] = "/mnt/c/wsl/projects/pythonise/tutorials/flask_series/app/app/static/img/uploads" @app.route("/upload-image", methods=["GET", "POST"]) def upload_image(): if request.method == "POST": if request.files: image = request.files["image"] image.save(os.path.join(app.config["IMAGE_UPLOADS"], image.filename)) print("Image saved") return redirect(request.url) return render_template("public/upload_image.html")
Bogotobogo
bogotobogo.com › python › Flask › Python_Flask_Blog_App_Tutorial_5.php
Flask blog app tutorial 5 : Uploading an image - 2020
Let's start by modifying our "add blog" page to include an option to upload an image. First, we'll modify the form-horizontal to a vertical form, so remove the class form-horizontal from the form. ... <!DOCTYPE html> <html lang="en"> <head> <title>Python Flask Blog App</title> <link ...
GitHub
github.com › teamsoo › flask-api-upload-image
GitHub - teamsoo/flask-api-upload-image: Flask API upload image and save to file.
Starred by 22 users
Forked by 16 users
Languages Python 79.5% | HTML 20.5% | Python 79.5% | HTML 20.5%
Geek Python
geekpython.in › render-images-from-flask
Upload and Display Images On the Frontend Using Python Flask
August 15, 2023 - In this article, you’ll learn the methods for handling the process of displaying and uploading images in Flask. ... Displaying local images – Since local images are already present in our storage system, so you’ll see the methods for displaying them on the client side. Uploading and displaying the dynamic images – In this segment, you’ll see the process of uploading the image to your specified folder and then displaying that uploaded image from the folder. Python, Flask, and Jinja have been used in this article, so you should have a basic knowledge of all of them because Python has been used to write the code and Jinja has also been used in some other parts.
Medium
rebecanrod.medium.com › flask-api-and-python-upload-files-bab88925ffa3
Flask API and Python — Upload Files | by Rebeca Nunes Rodrigues, MSc Student | Medium
April 15, 2025 - {% block title %}Upload{% endblock %}{% block main %}<div class="container"> <div class="row"> <div class="col"><h1>Upload an image</h1> <hr><form action="/upload-image" method="POST" enctype="multipart/form-data"><div class="form-group"> <label>Select image</label> <div class="custom-file"> <input type="file" class="custom-file-input" name="image" id="image"> <label class="custom-file-label" for="image">Select image...</label> </div> </div><button type="submit" class="btn btn-primary">Upload</button></form></div> </div> </div>{% endblock %} from flask import Flask, jsonify, request, render_template, redirect...@app.route("/upload-image", methods=["GET", "POST"]) def upload_image(): if request.method == "POST": if request.files: image = request.files["image"] print(image) return redirect(request.url) return render_template("upload_image.html")
pytz
pythonhosted.org › Flask-Uploads
Flask-Uploads — Flask-Uploads v0.1 documentation
Flask-Uploads allows your application to flexibly and efficiently handle file uploading and serving the uploaded files. You can create different sets of uploads - one for document attachments, one for photos, etc.
GitHub
gist.github.com › greyli › ca74d71f13c52d089a8da8d2b758d519
Photo upload and manage with Flask and Flask-Uploads (Multiple file upload support!). · GitHub
@app.route('/', methods=['GET', 'POST']) def upload_file(): form = UploadForm() if form.validate_on_submit(): for filename in request.files.getlist('photo'): str_name='admin' + str(int(time.time())) name = hashlib.md5(str_name.encode("utf-8")).hexdigest()[:15] photos.save(filename, name=name + '.') success = True else: success = False return render_template('index.html', form=form, success=success)
GitHub
gist.github.com › kylehounslow › 767fb72fde2ebdd010a0bf4242371594
Send and receive images using Flask, Numpy and OpenCV · GitHub
def post_image(img_file): """ post image and return the response """ img = open(img_file, 'rb').read() response = requests.post(URL, data=img, headers=headers) return response ... Sometimes you might not even have a code running at the other end to imdecode which is exactly what happened with me. Where I had to upload an image directly onto google-cloud-storage Here's a solution I came up with
Medium
medium.com › @brodiea19 › flask-sqlalchemy-how-to-upload-photos-and-render-them-to-your-webpage-84aa549ab39e
Flask SQLAlchemy: How to upload photos and render them to your webpage | by Brodie Ashcraft | Medium
October 9, 2023 - This is so we don’t allow for file types that are not images for security purposes. Now I move into my app.py file where I have defined all my Flask routes and where we are going to receive our file uploads and save them. We will first need to import some some built in Python packages to ...
TutorialsPoint
tutorialspoint.com › flask › flask_file_uploading.htm
Flask File Uploading
Handling file upload in Flask is very easy. It needs an HTML form with its enctype attribute set to multipart/form-data, posting the file to a URL. The URL handler fetches file from request.files[] object and saves it to the desired location.
YouTube
youtube.com › watch
How to Upload and Store Images (In the DB) with Python (Flask) (2020) - YouTube
How to Upload and Store images in the database with NodeJS, Express and Knex!-------------------------------------Code: https://github.com/Vuka951/tutorial-c...
Published June 19, 2020
GeeksforGeeks
geeksforgeeks.org › python › how-to-upload-file-in-python-flask
How to Upload File in Python-Flask - GeeksforGeeks
1 week ago - The name of the objective file can be obtained by using the following code and then we will save the uploaded file to the root directory. ... from distutils.log import debug from fileinput import filename from flask import * app = Flask(__name__) @app.route('/') def main(): return render_template("index.html") @app.route('/success', methods = ['POST']) def success(): if request.method == 'POST': f = request.files['file'] f.save(f.filename) return render_template("Acknowledgement.html", name = f.filename) if __name__ == '__main__': app.run(debug=True)