-2

I am trying to convert an API created in Flask into FastAPI, which demonstrates how to download a file from the server; however, I don't know how to change the code below:

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

Thanks in advance.

Chris
  • 18,724
  • 6
  • 46
  • 80
  • It's a very easily google-able problem. Here's one link that comes up and solves your problem after googling for 3 seconds: https://betterprogramming.pub/migrate-from-flask-to-fastapi-smoothly-cc4c6c255397 – Shubham Periwal Apr 28 '21 at 07:01
  • Does this answer your question? [How to Download a File after POSTing data using FastAPI?](https://stackoverflow.com/questions/73234675/how-to-download-a-file-after-posting-data-using-fastapi) – Chris Dec 27 '22 at 11:25
  • Related answers can also be found [here](https://stackoverflow.com/a/72053557/17865804), as well as [here](https://stackoverflow.com/a/73843234/17865804) and [here](https://stackoverflow.com/a/73241648/17865804). – Chris Dec 27 '22 at 11:26

1 Answers1

2

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")
HLX Studios
  • 213
  • 1
  • 3