angularjs:下载的文件名是"未知"使用Safari

时间:2017-05-12 20:37:56

标签: javascript angularjs safari

在Safari中下载文件,文件名为"未知" 这是我的下载代码 function downloadCSV(csv,filename){

var downloadContainer = angular.element(''),       content ='数据:attachment / csv,' + encodeURI(csv);

  // var downloadLink = angular.element('<a></a>'),
  var downloadLink = angular.element(downloadContainer.children()[0]),
      blob = new Blob([csv], {
        type: 'text/csv;charset=utf-8;'
      });

  if (window.navigator.msSaveOrOpenBlob) {
    // download file for IE
    navigator.msSaveBlob(blob, filename);
  }
  else {
    downloadLink.attr('href',  content);
    downloadLink.attr('download', filename);
    downloadLink.attr('style', 'display: none');
    // workaround for Safari version that not supporting download feature.
    downloadLink.attr('target', '_blank');

    angular.element(document).find('body').append(downloadContainer);

    // need timeout here since appending DOM takes some time
    $timeout(function() {
      downloadLink[0].click();
      downloadLink.remove();
    }, 10);
  }
}

并使用

调用此函数
downloadCSV(cvs, 'myDownloadFile.csv') 

但是使用Safari下载文件是&#34;未知&#34;文件。这个问题是否有任何解决方法。 BTW - 哪个版本的Safari不支持下载功能。

由于

0 个答案:

没有答案