2

In FastAPI I know that I can return a file (in FileResponse) by passing its path and filename as parameters. Is there any way I can return the file directly without having to save it locally and use the path?

Here's a part of the code for reference:

    new_path = path.join(getcwd(), "output", new_filename)
    return FileResponse(path = new_path, filename = new_filename)
Sara Karam
  • 21
  • 3
  • Related https://stackoverflow.com/questions/63465155/how-to-return-file-from-memory-in-fastapi-streamingresponse – alex_noname Aug 26 '22 at 08:25
  • 1
    A `StreamingResponse` accepts a file-like interface, so you should be able to return just that: https://fastapi.tiangolo.com/advanced/custom-response/#using-streamingresponse-with-file-like-objects – MatsLindh Aug 26 '22 at 08:29
  • similar problem was many times on Stackoverflow - especially for Flask - and for different file - images, docs, etc. You can use `io.BytesIO` or `io.StringIO` to create file-like object in memory. But not all functions can work with file-like objects instead of filename – furas Aug 26 '22 at 10:04
  • Please have a look at [this](https://stackoverflow.com/a/71728386/17865804), as well as [this](https://stackoverflow.com/a/71643439/17865804) and [this](https://stackoverflow.com/a/71639658/17865804) answer. – Chris Aug 27 '22 at 06:01

0 Answers0