当前位置:网站首页>How fastapi responds to file downloads

How fastapi responds to file downloads

2022-06-13 11:33:00 Mad devil coding King

fastapi Want to respond to file download , Need from starlette.responses Import FileResponse

from starlette.responses import FileResponse

And before that, you have to install aiofiles Dependency package

pip install aiofiles

The complete code is as follows :
FileResponse The first parameter is the path of the file , The second parameter indicates the name of the file displayed when downloading

import uvicorn

from fastapi import FastAPI
from starlette.responses import FileResponse


app = FastAPI()


@app.get("/file")
def file():
    return FileResponse('./demo.md', filename='demo.md')


if __name__ == '__main__':
    uvicorn.run('main:app', port=5555, reload=True)

原网站

版权声明
本文为[Mad devil coding King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131115375624.html