小编Ari*_*ric的帖子

使用fastapi上传文件

我根据官方文档使用fastapi上传文件,就像:

@app.post("/create_file/")
async def create_file(file: UploadFile=File(...)):
      file2store = await file.read()
      # some code to store the BytesIO(file2store) to the other database
Run Code Online (Sandbox Code Playgroud)

当我使用 python requests lib 发送请求时:

f = open(".../file.txt", 'rb')
files = {"file": (f.name, f, "multipart/form-data")}
requests.post(url="SERVER_URL/create_file", files=files)
Run Code Online (Sandbox Code Playgroud)

file2store 始终为空。有时(很少见),它可以获取文件字节,但几乎所有时间都是空的,所以我无法在另一个数据库上恢复文件。我还尝试了 'bytes' 而不是 'UploadFile',我得到了相同的结果。我的代码有什么地方不对,还是我使用fastapi上传文件的方式有问题?我google了很长时间,但没有成功。所以我在这里提出问题,希望知道答案的人可以帮助我。谢谢

python file-upload fastapi

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

fastapi ×1

file-upload ×1

python ×1