mia*_*mia 5 python flask angularjs 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
Run Code Online (Sandbox Code Playgroud)
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');
});
Run Code Online (Sandbox Code Playgroud)
还尝试使用 send_file() 作为替代方法,但它给出了相同的错误!
send_file(output, attachment_filename='project_data.xlsx',as_attachment=True)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
877 次 |
最近记录: |