FastAPI文件上传方法详解 - message"
UploadFile
首先,我们需要安装FastAPI和Uvicorn:
pip install fastapi uvicorn
main.py
from fastapi import FastAPI, File, UploadFile
import os
app = FastAPI() 拼多多帮助力入口
@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
file_path = os.path.join(os.getcwd(), file.filename)
with open(file_path, "wb") as f:
for chunk in await file.read():
f.write(chunk)
return {"message": "文件上传成功"}
upload_fileUploadFilefileos.getcwd()
要运行这个应用程序,我们需要安装Uvicorn:
uvicorn main:app --reload
http://127.0.0.1:8000/upload,FastAPI文件上传方法详解