当您从服务器获得blob响应时,如何填充提示浏览器窗口?

时间:2016-04-25 14:40:54

标签: javascript angularjs export-to-excel

我在kendo网格上有exportChallenges按钮,通过使用angularJs factory将网格数据导出为ex​​cel,我从服务器端收到了休息服务响应(Blob),但它没有提示用户在浏览器上保存,打开或下载选项。

如何使用Angularjs或本机javascript解决此问题?

export.js

$scope.exportChallenges = function() {
      processFactory.exportPrcChallenges($stateParams.processId, challengeType);
      .success(function(response) {
          var blob = new Blob([response.data], {
              type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          });
          debugger;
          var objectUrl = URL.createObjectURL(blob);
          window.open(objectUrl);
      });
  };

1 个答案:

答案 0 :(得分:1)

打开blob取决于浏览器(即,IE在API中有自己的实现)。这样做:

var blob = new Blob([response.data], {type: contentType});

//call the save blob API in IE
if(window.navigator && window.navigator.msSaveOrOpenBlob) {
    //save or open prompt in IE
    //there's a save prompt too, depending on what you need
    window.navigator.msSaveOrOpenBlob(blob, "filename");
} else { //other browsers
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
}