简体   繁体   中英

Change api of Flask to Fastapi

I am changing an API created in a flask to FastAPI, but I don't know how to change this code, any advice:

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

Thanks in advance.

It all depends of what kind of file you're trying to download. But you can find good information here: FastApi Streaming Response

In your case it would be something like:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM