How to Upload File in Python-Flask

Last Updated : 9 Jun, 2026

Flask simplifies file uploads by providing tools to receive and process files submitted through HTML forms. Uploaded files can be accessed using the request.files object, validated if required, and saved to a specified location on the server for further use.

Install the Flask using following command in terminal:

pip install flask

Stepwise Implementation

Step 1: A new folder "file uploading" should be created. Create the folders "templates" and "main.py" in that folder, which will store our HTML files and serve as the location for our Python code.

Step 2: For the front end, we must first develop an HTML file where the user can select a file and upload it by clicking the upload buttons. The user will click the submit button after choosing the file from their local computer in order to transmit it to the server.

Index.html

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

Explanation: The multipart/form-data encoding type allows files and form data to be transmitted to the server as part of the same request.

Step 3: We must make another HTML file just for acknowledgment. Create a file inside the templates folder called "Acknowledgement.html" to do this. This will only be triggered if the file upload went smoothly. Here, the user will receive a confirmation.

Acknowledgement.html

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

Step 4: Now inside the 'main.py' write the following codes. 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.

main.py

Python
from distutils.log import debug
from fileinput import filename
from flask import *  
from werkzeug.utils import secure_filename
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']
        filename = secure_filename(f.filename)
        f.save(filename)  
        return render_template("Acknowledgement.html", name=filename)  

if __name__ == '__main__':  
    app.run(debug=True)  

Output:

Run the following command in your terminal.

python main.py

Explanation:

  • /success route processes the uploaded file when the form is submitted using the POST method.
  • Uploaded file is retrieved from the request using request.files['file'].
  • secure_filename() function removes unsafe characters from the filename and generates a safe name for storage.
  • sanitized file is saved to the server using the save() method.
  • filename is passed to the acknowledgment page using render_template().
  • acknowledgment page displays a success message along with the name of the uploaded file.
  • Using secure_filename() helps prevent issues caused by unsafe or malicious filenames during file uploads.

Step 5: Now, to check if it is correctly working or not go to the folder where 'main.py'. Check in that folder you will find the files there.

Flask - File Upload
File structure
Comment