[FastAPI-28]上传多个文件

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)}
posted @ 2023-03-26 17:57  LeoShi2020  阅读(37)  评论(0编辑  收藏  举报