You want to save an uploaded file immediately and then process it in the background. Which FastAPI code snippet correctly implements this pattern?
Aasync def upload(file: UploadFile, background_tasks: BackgroundTasks):
background_tasks.add_task(process_file, file)
return {"message": "Processing started"}
Basync def upload(file: UploadFile):
contents = await file.read()
with open(f"saved_{file.filename}", "wb") as f:
f.write(contents)
process_file(f"saved_{file.filename}")
return {"message": "File saved and processed"}
Casync def upload(file: UploadFile, background_tasks: BackgroundTasks):
contents = await file.read()
with open(f"saved_{file.filename}", "wb") as f:
f.write(contents)
background_tasks.add_task(process_file, f"saved_{file.filename}")
return {"message": "File saved and processing started"}
Dasync def upload(file: UploadFile, background_tasks: BackgroundTasks):
background_tasks.add_task(process_file, file.file.read())
return {"message": "Processing started"}