Recommand · June 17, 2021 0

How do I return a xlsx file in fastAPI?

How do I return a xlsx file in fastAPI?

Using the python module fastAPI, I can’t figure out how to return a xlsx file.
Alse I use openpyxl library to edit my xlsx.

@router.get('/omission-download')
async def omissionFileDownload():
    file_like = open('./upload/test2.xlsx', mode="r") 
    return StreamingResponse(file_like, media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

And here is my error

  File "/usr/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 12: invalid continuation byte

If anyone knows what to do, I would appreciate it if you could tell me how.