6

I want to give xlsx on request. With using BytesIO and xlsxwriter I create a file.

Using the code below, I can download an empty(!) .txt file:

@router.get("/payments/xlsx", response_description='xlsx')
async def payments():
    """sss"""
    output = BytesIO()
    workbook = xlsxwriter.Workbook(output)
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, 'ISBN')
    worksheet.write(0, 1, 'Name')
    worksheet.write(0, 2, 'Takedown date')
    worksheet.write(0, 3, 'Last updated')
    workbook.close()
    output.seek(0)
    return StreamingResponse(output)

If I add headers={'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'} I get this error in browser:

Unable to open file
You may be having a problem connecting with the server, or the file that you wanted to open was corrupted.

How I can fix this?

Fyzzys
  • 756
  • 1
  • 7
  • 13
  • 1
    Since the entire file data are already loaded into memory, you shouldn't be using `StreamingResponse`. Please have a look at [this answer](https://stackoverflow.com/a/71643439/17865804) and [that answer](https://stackoverflow.com/a/71639658/17865804) on how to return a custom `Response` and set the `Content-Disposition` header. – Chris Aug 17 '22 at 18:40

1 Answers1

13

You have to set Content-Disposition header in the response

@router.get("/payments/xlsx", response_description='xlsx')
async def payments():
    output = BytesIO()
    workbook = xlsxwriter.Workbook(output)
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, 'ISBN')
    worksheet.write(0, 1, 'Name')
    worksheet.write(0, 2, 'Takedown date')
    worksheet.write(0, 3, 'Last updated')
    workbook.close()
    output.seek(0)

    headers = {
        'Content-Disposition': 'attachment; filename="filename.xlsx"'
    }
    return StreamingResponse(output, headers=headers)
JPG
  • 82,442
  • 19
  • 127
  • 206