将 Flask api 更改为 Fastapi

Change api of Flask to Fastapi

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

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

提前致谢。

这完全取决于您要下载的文件类型。但是您可以在这里找到有用的信息: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")