How to send and download a file in FastAPI


Working with files is found on many sites, so I decided to write this article with a brief but informative content.

Code examples can be used as a basis, which, if necessary, can be easily extended for your specific task without any difficulties.

I’m not going to write a long preface, let’s get down to the analysis.

  1. File download

Downloading a file is very simple, in fact, in one line:

from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()


@app.get("/file/download")
def download_file():
  return FileResponse(path="data.xlsx", filename="Статистика покупок.xlsx", media_type="multipart/form-data")

We import FileResponseand return it with three parameters(filename and media_type optional)

First parameter path points to the file that will be downloaded by the user, it can be either a string or os.PathLike.

In this case, I specified a relative path, my file data.xlsx lies next to the executable file.

Parameter filename determines the name of the output file, that is, with what name the user will download it, a fairly convenient functionality that allows you to store a file with a name that is convenient for us, while giving it to the user with a beautiful name.

And the last option I used is media_typeit points to a MIME type, in this case multipart/form-dataindicating that we are expecting a dataset from an HTML form.

The file can be absolutely anything, I used an Excel file as an example.

2. How to send a file to the server?

Sending a file is no more difficult than downloading.

We need to download the module python-multipart

$ pip install python-multipart

We have 2 ways to work with the received file:

By using File and UploadFile

from fastapi import FastAPI, UploadFile, File

app = FastAPI()


@app.post("/file/upload-bytes")
def upload_file_bytes(file_bytes: bytes = File()):
  return {'file_bytes': str(file_bytes)}


@app.post("/file/upload-file")
def upload_file(file: UploadFile):
  return file

In the method with File we get only a byte string as output, which I converted to a string type, for output in the response documentation.

This method is used only when we don’t need anything other than the contents of the file, we transfer bytes to Pandas (if we have an Excel format), for example, and then we start working with it directly.

Method UploadFile somewhat more interesting in terms of parameters, it has a whole set of them.

Do you want to take the name of the sent file? There is a method file.filename.

Need the file itself, not its bytes? Method file.file.

Need bytes? Please, file.file.read().

There are also others that I decided not to list, since I made a short guide, and not a rewrite of the documentation (the link to the page of which I left at the end of the article)

Well, just file returns us a dictionary of various data about the file

Here is such a short article for a quick start, for more detailed information I propose to refer to documentation.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *