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

时间:2018-04-08 22:46:36

标签: python angularjs flask xlsxwriter filesaver.js

我正在尝试下载由XlsxWriter创建的Excel文件,将其作为Flask视图的响应发送到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:

   $http(
        {
           method: 'POST',
           url: '/export',  /*You URL to post*/
           data: data,
           headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/vnd.openxmlformats 
                        officedocument.spreadsheetml.sheet'
            }
            }).then(function(response) {
            var blob = new Blob([response.data], 
                                {type:'application/vnd.openxmlformats- 
                                      officedocument.spreadsheetml.sheet'});
            FileSaver.saveAs(blob, 'project_data.xlsx')
            }, function(response) {
            $window.alert('Export failed');
        });

还尝试使用send_file()作为替代,但它也给出了同样的错误!

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

0 个答案:

没有答案