[FastAPI-26]Form表单接收数据,小文件上传

发布时间 2023-03-26 17:08:10作者: LeoShi2020
from fastapi import FastAPI,Form,File,UploadFile

app = FastAPI(title="Form表单")

'''
Form表单接收数据
'''

@app.post("/login",summary="登录")
def login(username : str = Form(description="用户名"),
          password : str = Form(description="密码")):
    return {
        "username": username,
        "password": password
    }

@app.post("/upload",summary="上传文件")
# 这种方式适合小文件,写入内存的
def upload_file(file : bytes = File(description="上传的文件")):
    print(file)
    with open("a.txt","wb") as f:
        f.write(file)
    return file