How to Upload Files With Python Flask

In this Python Flask article we are going to learn How to Upload Files With Python Flask ,

so for this purpose we are using Flask-Uploads library. 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. – and the application

can be configured to save them all in different places and to generate different URLs for them.

 

 

 

Learn How to Work with Flask Login and Flask Logout 

 

 

 

Installation 

So first of all you need to install Flask-Uploads, you can easily use pip for the installation.

 

 

 

 

OK after installation, you need to create a python file at name of app.py, and add these codes in

that file. there are some imports from flask_uploads that we will need in this article. after that you

can see that we have Upload Sets, so an “upload set” is a single collection of files. and you can see

that we have just one upload set. now you need to configure the location of the uploaded image 

for example in here when ever i upload the image it will be saved in the static/images folder.

 

 

 

 

 

Now you need to create a templates folder because we want to add our template, we have

just one template that is upload.html.

 

 

templates/upload.html

 

 

 

 

 

Also you need to create an static folder and in the static folder create another folder at name

of images, because after uploading the image we want to save the image in that path.

 

 

 

This is our Project structure.

Flask Upload Structure
Flask Upload Structure

 

 

 

 

 

Now run your Flask Project and go to http://localhost:5000/ 

How to Upload Files With Python Flask
How to Upload Files With Python Flask

 

 

 

 

 

After that choose a file and upload that , this will be the result, you will receive the uploaded image

or file name back.

Flask Uploading Image
Flask Uploading Image

 

 

 

 

 

And if you check your static/images, you will see your uploaded image in their.

Flask Uploaded Image Structure
Flask Uploaded Image Structure

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

3 thoughts on “How to Upload Files With Python Flask”

Comments are closed.

Share via
Copy link
Powered by Social Snap
×