📖 Scenario: You are building a simple web app where users can upload a file. You want to save the uploaded file on the server so you can use it later.
🎯 Goal: Create a Flask app that accepts a file upload from a user and saves the file to a folder on the server.
📋 What You'll Learn
Create a Flask app with a route to upload files
Set a folder path to save uploaded files
Use Flask's request object to get the uploaded file
Save the uploaded file to the specified folder
💡 Why This Matters
🌍 Real World
Uploading and saving files is common in web apps for user profile pictures, documents, or media content.
💼 Career
Understanding file uploads is essential for backend web developers working with Flask or similar frameworks.
Progress0 / 4 steps
1
Set up Flask app and upload folder
Import Flask and request from flask. Create a Flask app called app. Set a variable UPLOAD_FOLDER to the string 'uploads/'.
Flask
Hint
Use Flask(__name__) to create the app. Set UPLOAD_FOLDER as a string with the folder name.
2
Create upload route and HTML form
Add a route /upload to the app that accepts GET and POST methods. Inside the route function upload_file, if the request method is GET, return a simple HTML form string with a file input named file and a submit button.
Flask
Hint
Use @app.route with methods=['GET', 'POST']. Return a form with enctype='multipart/form-data' and a file input named file.
3
Get uploaded file from request
Inside the upload_file function, after the GET block, add code for POST method. Get the uploaded file from request.files using the key 'file' and store it in a variable called uploaded_file.
Flask
Hint
Use request.files['file'] to get the uploaded file object.
4
Save the uploaded file
Still inside the upload_file function, after getting uploaded_file, save it to the folder UPLOAD_FOLDER using the save() method. Use uploaded_file.filename to get the original filename. Return a string 'File saved successfully' after saving.
Flask
Hint
Use uploaded_file.save() with the path combining UPLOAD_FOLDER and uploaded_file.filename. Return a success message string.
Practice
(1/5)
1. In Flask, which object do you use to access files uploaded by a user in a form?
easy
A. request.args
B. request.files
C. request.data
D. request.form
Solution
Step 1: Understand Flask request objects
Flask provides different objects to access data sent by the client. request.files holds uploaded files.
Step 2: Identify correct object for files
request.form is for form fields, request.data is raw data, and request.args is for URL query parameters.
Final Answer:
request.files -> Option B
Quick Check:
Uploaded files = request.files [OK]
Hint: Uploaded files are always in request.files [OK]
Common Mistakes:
Using request.form to get files
Trying to access files from request.args
Confusing request.data with files
2. Which of the following is the correct way to save an uploaded file named file to a folder called uploads in Flask?
easy
A. file.save('uploads/' + file.filename)
B. file.save(file.filename)
C. save(file, 'uploads/')
D. request.save(file, 'uploads/')
Solution
Step 1: Use the file object's save method
The uploaded file object has a save() method that takes the full path to save the file.
Step 2: Provide correct path including folder and filename
Concatenate the folder path and the file's filename to save it in the right place.
Final Answer:
file.save('uploads/' + file.filename) -> Option A
Quick Check:
Use file.save(path) with full path [OK]
Hint: Use file.save('folder/' + file.filename) to save files [OK]
Common Mistakes:
Calling save() without a path
Using save() as a standalone function
Not including folder path in save()
3. What will happen when this Flask code runs after a file upload?