에듀테크 프로젝트 대시보드
폼 데이터와 파일 업로드
JSON 이 아닌 multipart/form-data 요청 — HTML 폼 전송과 파일 업로드를 다룹니다.
pip install python-multipart폼·파일 파싱에 필요합니다. fastapi[standard] 에는 포함되어 있습니다.
from typing import Annotated
from fastapi import Form
@app.post("/login")
def login(
username: Annotated[str, Form()],
password: Annotated[str, Form()],
):
...
# Content-Type: application/x-www-form-urlencoded 또는 multipart/form-datafrom fastapi import UploadFile
import shutil, pathlib
UPLOAD_DIR = pathlib.Path("uploads")
@app.post("/upload")
async def upload(file: UploadFile):
dest = UPLOAD_DIR / file.filename
with dest.open("wb") as buffer:
shutil.copyfileobj(file.file, buffer)
return {"filename": file.filename, "size": file.size, "type": file.content_type}await file.read() / await file.write() / await file.seek(0)bytes 로 받으면 전체가 메모리에 올라감 — 큰 파일은 UploadFile@app.post("/posts")
async def create_post(
title: Annotated[str, Form()],
files: list[UploadFile],
):
return {"title": title, "count": len(files)}from fastapi.staticfiles import StaticFiles
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")운영에서는 정적 파일을 Nginx 가 직접 서빙하는 편이 빠릅니다 (location /static/).