
Now navigate to the project’s root directory using command line tool and execute the command python main.py or simple main.py, your server will be started on default port 5000. You can use this page for uploading single file. This templates directory is the standard folder in the flask based web application. This is upload.html page kept under subdirectory – templates.

Now I need a template page for uploading file. # check if the post request has the file partįilename = secure_filename(file.filename)įile.save(os.path.join(app.config, filename))įlash('Allowed file types are txt, pdf, png, jpg, jpeg, gif') Return '.' in filename and filename.rsplit('.', 1).lower() in upload_form(): import osįrom flask import Flask, flash, request, redirect, render_templateįrom werkzeug.utils import secure_filenameĪLLOWED_EXTENSIONS = set()

I show success message on successful file upload. In the next section I will talk about this template file – upload.html. I have used upload.html page for uploading file to the desired directory. It defines all required URIs for performing file upload operations. This script is the perfect instance of Python flask file upload example. from flask import FlaskĪpp.config = UPLOAD_FOLDERĪpp.config = 16 * 1024 * 1024 File Upload Here I have also specified the file upload folder and maximum size of the file to be uploaded as 16MB. If you do not have this module then install it using the command pip install flask. In the below script I import the required module – Flask.

Here I need to assign a secret key otherwise flash will not work in Python. App ConfigurationĬreate the below app.py script(py is the extension to indicate Python script) where I need to import the flask module. I may not mention the project’s root directory name in the subsequent sections but I will assume that I am creating files with respect to the project’s root directory. Python 3.6.6 – 3.9.1, Flask 1.1.1 – 1.1.2 ( pip install flask) Project DirectoryĬreate a project root directory called python-flask-file-upload as per your chosen location.

Python Flask REST API Multiple Files Upload.Python Flask Multiple Files Upload Example.Python Flask REST API File Upload Example.In this file upload example I am going to show you how to select single file and upload in the server. You may also find useful example on file upload on different technologies. The tutorial, Python flask file upload example, will show you how to upload single file using Python 3 and Flask web framework.
