与 FASTAPI 一起使用时未定义名称 'File'

name 'File' is not define when used with FASTAPI

从 Insomnia/Postman API 开发环境发送 Post 请求时,以下函数应输出图像的文件大小。

@app.post("/user/photo")
async def upload_user_photo(response: Response, profile_photo: bytes = File(...)):
    response.headers["x-file-size"] = str(len(profile_photo))
    response.set_cookie(key="cookie-api", value="test")
    return {"file size": len(profile_photo)}

但是出现以下错误:

  File ".\run.py", line 54, in <module>
    async def upload_user_photo(response: Response, profile_photo: bytes = File(...)):
NameError: name 'File' is not defined

我读到 File() 已从 Python 3 开始贬值,那么有什么方法可以让它发挥作用?

Python file 函数已弃用。请记住,大写很重要。该代码需要的是来自 fastapi 的 File class。您需要添加:

from fastapi import File