How to Upload File in PythonFlask (original) (raw)

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 `

upload the file : GFG

`

**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 `

success

File uploaded successfully

File Name: {{name}}

`

**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:

**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