首页 > 解决方案 > 将 Flask 的 api 更改为 Fastapi

问题描述

我正在将烧瓶中创建的 API 更改为 FastAPI,但我不知道如何更改此代码,任何建议:

@app.route('/download/<fname>', methods=['GET'])
def download(fname):
 return send_file(fname) 

提前致谢。

标签: pythonfastapi

解决方案


这完全取决于您要下载的文件类型。但是你可以在这里找到很好的信息:FastApi Streaming Response

在您的情况下,它将类似于:

from fastapi.responses import StreamingResponse

@app.get("/download")
async def download(fname : str):
 file_like = open(fname, mode="rb")
 return StreamingResponse(file_like, media_type="type of your file")

推荐阅读