docs_src/custom_response/tutorial008_py310.py
raw| 1 | from fastapi import FastAPI |
| 2 | from fastapi.responses import StreamingResponse |
| 3 | |
| 4 | some_file_path = "large-video-file.mp4" |
| 5 | app = FastAPI() |
| 6 | |
| 7 | |
| 8 | @app.get("/") |
| 9 | def main(): |
| 10 | def iterfile(): # (1) |
| 11 | with open(some_file_path, mode="rb") as file_like: # (2) |
| 12 | yield from file_like # (3) |
| 13 | |
| 14 | return StreamingResponse(iterfile(), media_type="video/mp4") |