0
0
FastAPIframework~30 mins

Multiple file uploads in FastAPI - Mini Project: Build & Apply

Choose your learning style9 modes available
Multiple file uploads
📖 Scenario: You are building a simple web API using FastAPI that allows users to upload multiple files at once. This is useful for applications like photo galleries, document submissions, or any service that needs to accept several files in one request.
🎯 Goal: Create a FastAPI app that accepts multiple file uploads through a POST request and returns the filenames of the uploaded files.
📋 What You'll Learn
Use FastAPI to create the web API
Accept multiple files in a single POST request
Return a JSON list of the uploaded filenames
Use the correct FastAPI types for file uploads
💡 Why This Matters
🌍 Real World
Many web applications need to accept multiple files from users, such as photo upload sites, document management systems, or online forms.
💼 Career
Understanding how to handle multiple file uploads is a common requirement for backend developers working with web APIs.
Progress0 / 4 steps
1
Create FastAPI app and import UploadFile
Write code to import FastAPI and UploadFile from fastapi. Then create a FastAPI app instance called app.
FastAPI
Need a hint?

Use from fastapi import FastAPI, UploadFile and then app = FastAPI().

2
Add POST endpoint to accept multiple files
Add a POST endpoint at path /uploadfiles/ using @app.post. Define an async function called upload_files that accepts a parameter files typed as a list of UploadFile using files: List[UploadFile] and File(...) from fastapi. Import File from fastapi and List from typing as well.
FastAPI
Need a hint?

Use @app.post("/uploadfiles/") and define async def upload_files(files: List[UploadFile] = File(...)).

3
Extract filenames from uploaded files
Inside the upload_files function, create a list called filenames that contains the filename attribute of each file in files. Use a list comprehension with file.filename for each file in files.
FastAPI
Need a hint?

Use a list comprehension: filenames = [file.filename for file in files].

4
Return filenames as JSON response
Complete the upload_files function by returning a dictionary with a key filenames and the value as the filenames list.
FastAPI
Need a hint?

Return {"filenames": filenames} from the function.