📖 Scenario: You are building a simple Flask web app where users can upload files. After uploading, users should be able to access and download their files from the server.
🎯 Goal: Build a Flask app that serves uploaded files from a specific folder so users can download them by visiting a URL.
📋 What You'll Learn
Create a Flask app with an upload folder path
Set a configuration variable for the upload folder
Write a route to serve files from the upload folder
Use Flask's send_from_directory function to serve files
💡 Why This Matters
🌍 Real World
Web apps often let users upload files like images or documents. Serving these files back to users requires safely sending files from a server folder.
💼 Career
Knowing how to serve uploaded files is essential for backend web developers working with Flask or similar frameworks.
Progress0 / 4 steps
1
Set up the upload folder path
Create a variable called UPLOAD_FOLDER and set it to the string 'uploads'.
Flask
Hint
This variable will tell Flask where to find the uploaded files.
2
Configure the Flask app to use the upload folder
Add a line to set app.config['UPLOAD_FOLDER'] equal to the variable UPLOAD_FOLDER.
Flask
Hint
This tells Flask where to look for uploaded files when serving them.
3
Create a route to serve uploaded files
Write a Flask route with the URL pattern /uploads/<filename> that uses a function named uploaded_file with a parameter filename.
Flask
Hint
This route will handle requests to get files by their filename.
4
Serve the file using send_from_directory
Inside the uploaded_file function, return the result of calling send_from_directory with app.config['UPLOAD_FOLDER'] and filename as arguments.
Flask
Hint
This function sends the requested file from the upload folder to the user.
Practice
(1/5)
1. What is the main Flask function used to serve uploaded files from a specific folder?
easy
A. send_from_directory
B. render_template
C. redirect
D. url_for
Solution
Step 1: Understand serving files in Flask
Flask provides a function specifically to serve files from a folder safely, which is send_from_directory.
Step 2: Identify the correct function
render_template is for HTML templates, redirect sends users to a different URL, and url_for builds URLs. Only send_from_directory serves files.
Final Answer:
send_from_directory -> Option A
Quick Check:
Serving files = send_from_directory [OK]
Hint: Serving files? Use send_from_directory [OK]
Common Mistakes:
Confusing send_from_directory with render_template
Trying to serve files with redirect
Using url_for to serve files directly
2. Which of the following is the correct syntax to serve a file named example.txt from the folder uploads in Flask?
easy
A. redirect('uploads/example.txt')
B. send_file('uploads/example.txt')
C. send_from_directory('uploads', 'example.txt')
D. url_for('uploads/example.txt')
Solution
Step 1: Recall the function signature
send_from_directory takes two arguments: the folder name and the filename.
Step 2: Match the syntax
send_from_directory('uploads', 'example.txt') correctly calls send_from_directory('uploads', 'example.txt'). Options A, B, and D use incorrect functions or formats.
Final Answer:
send_from_directory('uploads', 'example.txt') -> Option C
A. The folder and filename arguments are reversed in send_from_directory.
B. The route decorator syntax is incorrect.
C. The function name 'files' is a reserved keyword.
D. send_from_directory cannot be used inside a route.
Solution
Step 1: Check send_from_directory arguments
The function call uses send_from_directory(filename, 'uploads'), but the first argument should be the folder, second the filename.
Step 2: Confirm correct argument order
The correct call is send_from_directory('uploads', filename). The route and function name are valid, and send_from_directory is allowed in routes.
Final Answer:
The folder and filename arguments are reversed in send_from_directory. -> Option A
Quick Check:
send_from_directory(folder, filename) order matters [OK]
Hint: Folder first, filename second in send_from_directory [OK]
Common Mistakes:
Swapping folder and filename arguments
Thinking function names like 'files' are reserved
Believing send_from_directory can't be used in routes
5. You want to serve uploaded files securely from the folder user_uploads. Which Flask route setup correctly serves files while preventing directory traversal attacks?