0
0
Flaskframework~30 mins

Serving uploaded files in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Serving Uploaded Files with Flask
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

This function sends the requested file from the upload folder to the user.