Flask Xlsxwriter创建的excel文件作为响应发送时已损坏

2024-09-25 00:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图下载一个由XlsxWriter创建的Excel文件,将其作为Flask view的响应发送到AngularJS。但是,当我试图打开该文件时,它显示该文件已损坏。我不知道我错过了什么。请在下面找到我的代码:

XlsGenerator.py:

    output = io.BytesIO()
    workbook = xlsxwriter.Workbook(output, {'in_memory': True})
        # code to add data 
    workbook.close()
    output.seek(0)
    response = Response(output.getvalue(), mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                    headers={"Content-Disposition": "attachment;filename=Dss_project.txt"},
                    content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

    return response

app.js:

^{pr2}$

也尝试使用send_file()作为替代方法,但它给出了相同的错误!在

send_file(output, attachment_filename='project_data.xlsx',as_attachment=True)

Tags: 文件projecttrueoutputdataattachmentapplicationresponse