-1

Im trying to send an excel file back to the user's browser in flask. The excel file is created with openpyxl.

@app.route('/test', methods=['GET', 'POST'])
def test():
    if request.method == 'GET':

    ...

        buf = BytesIO()
        wb.save(buf)
        buf.seek(0)
        resp = make_response(buf)
            resp.headers["Content-Disposition"] = "attachment; filename=export.xlsx"
            resp.headers["Content-Type"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

        return resp

Im getting the following error:

TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a BytesIO.

How can i do this, without using BytesIO?

Many thanks in advance

pablowilks2
  • 299
  • 5
  • 15

1 Answers1

1

Try send_file:

from flask import send_file
#...
def test():
    buf = BytesIO()
    # ...
    resp = send_file(buf)
    resp.headers["Content-Disposition"] = "attachment; filename=export.xlsx"
    resp.headers["Content-Type"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Myk Willis
  • 12,306
  • 4
  • 45
  • 62