代码如下:
from fastapi import FastAPI, File, UploadFile, Form
app = FastAPI()
@app.post("/uploadfile/")
def create_upload_file(file: UploadFile = File(...), description: str = Form(...)):
print(description)
# 保存文件
f = open("temp.json", 'wb')
data = file.file.read()
f.write(data)
f.close()
with open("temp.json", "r", encoding='utf-8') as f:
table_dict = json.load(f)
在这个例子中,create_upload_file函数接收一个file参数和一个description参数。file参数使用UploadFile类型,用于接收文件。description参数使用Form类型,用于接收字符串。通过这种方式,你可以同时接收文件和字符串。