Uploading files using the Flask framework is a straightforward task. You need an HTML form with the enctype attribute and the URL manager, which retrieves the file and saves the object in the desired location. These files are temporarily stored on the server and then in the desired location.

Or we can say that File Uploading is the process of transmitting binary or standard files to the server. Flask makes it easy for us to upload files. All we need is to have an HTML form with a set of multipart / form-data encryption.

The server-side globe script retrieves the requested object file using the request — files [] object. Once the file is loaded correctly, it is saved in the desired location on the server.

The uploaded file is saved in the temporary directory of the server for a while before being saved in the desired location.

However, we can mention the path to the folder in which the file should be uploaded to the server, and the maximum size of the uploaded file should also be mentioned. This can be done in the configuration settings of the global object.

app.config[‘UPLOAD_FOLDER’]      = it is used to  specify the location or the upload folder.  

We can easily understand the  file uploading with the help of an example

In this example, we will provide a file selector (file_upload_form.html) to the user where the user can select a file from the file system and send it to the server.

On the server-side, the file is retrieved using the request.files [‘file’] object and saved in the server location.




Now let’s run the uploadpagefile.py.

An HTML form is displayed to the user.

Click on the Browse button so that we can explore the file system and search the appropriate file.

Flask File Uploading

Now we have to choose a file, as shown below.

Flask File Uploading2

Now double click on the file.

Flask File Uploading3

Here click on the Upload button. Then user get a message of successful file uploading as given below:-

Flask File Uploading4

We can confirm this by checking in the directory where upload.py is located, as shown in the image below.

Flask File Uploading4

Pin It on Pinterest

Share This