Send temporary xlsx file for download with Python Falcon

696 Views Asked by At

The client makes a post request to which I should respond with a .xlsx file generated by the data I've been given. Data arrives perfectly, I handle it and it sees like the .xlsx file is sent but the client is unable to handle it. I want the download window to popup and download the file but nothing happens. I can't see if the problem lies on the front-end or in my API, since I create the file in-memory.

Here is the part the file is sent:

excel_filename = name + '.xlsx'
output = BytesIO()
wb = xlsxwriter.Workbook(output, {'in_memory': True})
ws = wb.add_worksheet()
row = col = 0
for colName in columnNames:
  ws.write(row, col, colName)
  col += 1
for r in rowData:
  col = 0
  row += 1
  for cellValue in r:
    ws.write(row, col, cellValue)
    col += 1
wb.close()

xlsx_file = output.getvalue()

resp.set_header("Content-Disposition", "attachment; filename=\"%s\"" % excel_filename)
resp.data = xlsx_file

Here is client's request:

this.client.post('/export-report-excel', { data })

I've been searching a couple days but couldn't find anything that solves my problem.

EDIT: Console displays

Error: Parser is unable to parse the response

1

There are 1 best solutions below

0
On

I think you have to add the

Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

header indicating the mime type of your xlsx file