import typing
from fastapi import FastAPI, File, UploadFile
app = FastAPI(title="Form表单")
'''
上传多个文件
'''
@app.post("/files",summary="通过内存缓存上传多个文件")
def create_files(files:typing.List[bytes] = File()):
return {"file_sizes":[len(file) for file in files]}
@app.post("/files2",summary="上传2个文件")
def create_files2(file1:bytes =File(),
file2:bytes = File()):
return {"file1_size":len(file1),
"file2_size":len(file2)}
@app.post("/upload_files", summary="不通过内存缓存上传多个文件")
def upload_files(files: typing.List[UploadFile]):
return {"filenames":[file.filename for file in files]}
@app.post("/upload_files2", summary="不通过内存缓存上传2个文件")
def upload_files2(file1: typing.Optional[UploadFile],
file2: bytes = File()):
return {"file1_name":file1.filename,
"file2_size":len(file2)}