2016-04-25 52 views
1

我有exportChallenges kendo網格上的按鈕,通過使用angularJs工廠將網格數據導出爲ex​​cel我從服務器端收到休息服務響應(Blob),但它不會提示用戶在瀏覽器上保存,打開或下載選項。如何填充提示瀏覽器窗口,當你有來自服務器的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

打開斑點取決於瀏覽器(即,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); 
} 
相關問題