Javascript 从服务器下载文本/csv内容作为文件,但在Mozilla FireFox中不起作用

Javascript 从服务器下载文本/csv内容作为文件,但在Mozilla FireFox中不起作用,javascript,angularjs,node.js,csv,firefox,Javascript,Angularjs,Node.js,Csv,Firefox,答复如下: $http({method'GET',url:'/someUrl'})。 成功(函数(数据、状态、标题、配置){ var anchor=角度元素(“”); anchor.attr({ href:'data:attachment/csv;charset=utf-8,'+encodeURI(数据), 目标:“\u blank”, 下载:“filename.csv” })[0]。单击(); }). 错误(函数(数据、状态、标题、配置){ //如果有错误,你应该在这里看到 }); 我使用a

答复如下:

$http({method'GET',url:'/someUrl'})。
成功(函数(数据、状态、标题、配置){
var anchor=角度元素(“”);
anchor.attr({
href:'data:attachment/csv;charset=utf-8,'+encodeURI(数据),
目标:“\u blank”,
下载:“filename.csv”
})[0]。单击();
}).
错误(函数(数据、状态、标题、配置){
//如果有错误,你应该在这里看到
});
我使用angular实现了这个从服务器下载文件到客户端的解决方案。这是完美的在Google chrome中运行良好。但是这个解决方案在Mozilla Firefox中不起作用


谢谢

您必须先将您创建的锚连接到文档。添加以下内容:

var anchor = angular.element('<a/>');
anchor.css({display: 'none'}); // Make sure it's not visible
angular.element(document.body).append(anchor); // Attach to document

anchor.attr({
    href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
    target: '_blank',
    download: 'filename.csv'
})[0].click();

anchor.remove(); // Clean it up afterwards

var-anchor=angular.element(“

您应该使用
encodeURIComponent()
而不是
encodeURI()
——因为像#,&,=”这样的字符的编码更好。请参阅的文档,我的解决方案是(按照maciejlesniak的建议使用encodeURIComponent):

$http({method'GET',url:'/someUrl'})。
成功(函数(数据、状态、标题、配置){
var anchor=角度元素(“”);
css({display:'none'});//确保它不可见
angular.element(document.body).append(anchor);//附加到文档
anchor.attr({
href:'data:attachment/csv;charset=utf-8,'+encodeURIComponent(数据),
目标:“\u blank”,
下载:“filename.csv”
})[0]。单击();
anchor.remove();//之后将其清理干净
}).
错误(函数(数据、状态、标题、配置){
//如果有错误,你应该在这里看到
});

您也可以解决类似的问题

var anchor = angular.element('<a/>');
anchor.attr({
  href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
  target: '_blank',
  download: 'filename.csv'
});
var click = new MouseEvent('click', {
      'view': window,
      'bubbles': true,
      'cancelable': true
  }); 
anchor[0].dispatchEvent(click);
var-anchor=angular.element(“”);
anchor.attr({
href:'data:attachment/csv;charset=utf-8,'+encodeURI(数据),
目标:“\u blank”,
下载:“filename.csv”
});
var click=new MouseEvent('click'{
“视图”:窗口,
“泡沫”:没错,
“可取消”:true
}); 
锚定[0]。dispatchEvent(单击);
这应该也适用于Firefox,而无需在document.body上附加锚定标记

$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
var anchor = angular.element('<a/>');
anchor.css({display: 'none'}); // Make sure it's not visible
angular.element(document.body).append(anchor); // Attach to document

anchor.attr({
    href: 'data:attachment/csv;charset=utf-8,' + encodeURIComponent(data),
    target: '_blank',
    download: 'filename.csv'
})[0].click();

anchor.remove(); // Clean it up afterwards

  }).
  error(function(data, status, headers, config) {
    // if there's an error you should see it here
  });
var anchor = angular.element('<a/>');
anchor.attr({
  href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
  target: '_blank',
  download: 'filename.csv'
});
var click = new MouseEvent('click', {
      'view': window,
      'bubbles': true,
      'cancelable': true
  }); 
anchor[0].dispatchEvent(click);