0
0
Flaskframework~10 mins

File upload forms in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - File upload forms
User opens form page
Browser shows form with file input
User selects file and submits
Server receives POST request
Flask reads file from request.files
Server saves file or processes it
Server sends response back
Browser shows success or error message
This flow shows how a user selects a file in a form, submits it, and how Flask handles the file upload on the server side.
Execution Sample
Flask
from flask import Flask, request, render_template_string
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        f = request.files['file']
        f.save(f.filename)
        return 'File saved'
    return '''<form method='post' enctype='multipart/form-data'>
                <input type='file' name='file'>
                <input type='submit'>
              </form>'''
This Flask app shows a form to upload a file and saves the file on the server when submitted.
Execution Table
StepActionRequest MethodFile in request.filesFile saved?Response
1User opens pageGETNoneNoForm HTML sent
2User selects file and submitsPOSTFile object presentNoProcessing upload
3Server reads filePOSTFile object presentNoSaving file
4Server saves filePOSTFile object presentYesFile saved message sent
5User sees confirmationPOSTNoneYesFile saved
💡 After file is saved, server sends confirmation and stops processing.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
request.methodGETPOSTPOSTPOSTPOST
request.files['file']NoneFile objectFile objectFile objectNone
f.filenameNoneexample.txtexample.txtexample.txtNone
file saved?NoNoNoYesYes
Key Moments - 3 Insights
Why do we need enctype='multipart/form-data' in the form?
Without enctype='multipart/form-data', the file data won't be sent in the request.files dictionary, so the server cannot access the uploaded file (see execution_table step 2).
What happens if the request method is GET?
The server just sends the form HTML without trying to read or save any file (see execution_table step 1).
How does Flask access the uploaded file?
Flask uses request.files['file'] to get the file object sent by the form input named 'file' (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of request.method at Step 3?
AGET
BPOST
CPUT
DDELETE
💡 Hint
Check the 'Request Method' column at Step 3 in the execution_table.
At which step does the file get saved on the server?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'File saved?' column in the execution_table to find when it changes to Yes.
If the form did not have enctype='multipart/form-data', what would happen at Step 2?
AFile would be in request.files
BRequest method would change to GET
CFile would be missing in request.files
DServer would save file automatically
💡 Hint
Refer to key_moments about enctype and execution_table Step 2.
Concept Snapshot
File upload forms in Flask:
- Use <form method='post' enctype='multipart/form-data'>
- Use <input type='file' name='file'> to select file
- Access file in Flask with request.files['file']
- Save file with file.save(filename)
- Respond with confirmation after saving
Full Transcript
This example shows how a Flask app handles file uploads. The user opens a page with a form that includes a file input. When the user selects a file and submits the form, the browser sends a POST request with the file data encoded as multipart/form-data. Flask reads the file from request.files using the input's name attribute. The server saves the file and sends back a confirmation message. The key is to have the correct form encoding and to access the file properly on the server side.