我正在尝试下载由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)