小编Mar*_*eco的帖子

快速 API - 如何在 GET 中显示来自 POST 的图像?

我正在使用 FastAPI 创建一个应用程序,该应用程序应该生成上传图像的调整大小版本。上传应该通过 POST/images 完成,在调用路径 /images/800x400 后,它应该显示来自数据库的随机图像,大小为 800x400。我在尝试显示图像时遇到错误。

 from fastapi.responses import FileResponse
 import uuid

 app = FastAPI()

 db = []

@app.post("/images/")
async def create_upload_file(file: UploadFile = File(...)):

    contents = await file.read() 

    db.append(file)

    with open(file.filename, "wb") as f:
        f.write(contents)

    return {"filename": file.filename}

@app.get("/images/")
async def show_image():  
     return db[0]
Run Code Online (Sandbox Code Playgroud)

作为回应我得到:

{
  "filename": "70188bdc-923c-4bd3-be15-8e71966cab31.jpg",
  "content_type": "image/jpeg",
  "file": {}
}
Run Code Online (Sandbox Code Playgroud)

我想使用: return FileResponse(some_file_path) 并在文件路径中输入上面的文件名。这是正确的思维方式吗?

python fastapi

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

标签 统计

fastapi ×1

python ×1