Flask File Uploading

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['MAX_CONTENT-PATH'] = it is used
to specify the maximum size of the file to be uploaded. 

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.

Fileuploadform1.html

<html>  
 <head>  
 <title>upload</title>  
 </head>  
 <body>  
 <form action = "/success4" method = "post" enctype="multipart/form-data">   
 <input type="file" name="file" />  
 <input type = "submit" value="Upload">  
 </form>  
 </body>  
 </html> 

Success4.html

<html>  
 <head>  
 <title>success</title>  
 </head>  
 <body>  
 <p>File  has been uploaded successfully</p>  
 <p>File Name: {{name}}</p>  
 </body>   
 </html> 

Uploladpagefile.py

from flask import *  
 app = Flask(__name__)  
 @app.route('/')  
 def upload():  
 return render_template("fileuploadform1.html")  
 @app.route('/success', methods = ['POST'])  
 def success():   
 if request.method == 'POST':  
 f = request.files['file']  
 f.save(f.filename)  
 return render_template("success4.html", name = f.filename)  
 if __name__ == '__main__':
 app.run(debug = True) 

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