我开发了用于下载excel文件的AngularJS代码。该代码在Chrome和IE中运行良好。但不适用于Firefox。响应来自服务器,没有任何问题。这是我的代码,
var did = "someid";
var url = '/downloadAsExcel';
$http({method: 'POST', url: url, data:did,headers: {'Content-type': 'application/json'},responseType: 'arraybuffer'})
.success(function(data, status, headers, config) {
var blob = new Blob([data],{type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = headers('Content-Disposition').split(";")[1];
fileName = fileName.trim().split("=")[1];
var downloadName = (fileName != 'undefined') ? fileName : "DefaultName";
link.download = downloadName;
link.click();
})
.error(function(data, status) {
console.log("Some thing went wrong "+status);
});
请帮忙,
谢谢, JK
答案 0 :(得分:1)
这是从Axel获得帮助后的解决方案。
var did = "someid";
var url = '/downloadAsExcel';
$http({method: 'POST', url: url, data:did,headers: {'Content-type': 'application/json'},responseType: 'arraybuffer'})
.success(function(data, status, headers, config) {
var blob = new Blob([data],{type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = headers('Content-Disposition').split(";")[1];
fileName = fileName.trim().split("=")[1];
var downloadName = (fileName != 'undefined') ? fileName : "DefaultName";
link.download = downloadName;
if(document.body != null){ document.body.appendChild(link); }
link.click();
})
.error(function(data, status) {
console.log("Some thing went wrong "+status);
});