2017-08-02 176 views
0

我對AngularJS非常陌生。我需要幫助才能擺脫下面提到的情況。我試圖在按鈕點擊的新標籤中打開一個PDF文件。我正在解決的代碼如下。新標籤沒有打開。任何指導將不勝感激。AngularJS在新選項卡中打開PDF

$http.get('/api/services/downloadpdf/' + scope.invoice.id, { responseType: 'arraybuffer' }).then(function (response) { 
    var file = new Blob([response.data], { type: 'application/pdf' }); 
    file.name = 'invoice.pdf'; 
    var fileUrl = URL.createObjectURL(file); 
    $window.open(fileUrl); 
}); 

回答

0

如果您有文件的鏈接只是給這樣的,在HTML

<a target="_target" href="invoice.pdf" role="button">View</a> 

在控制器使用該

$http.get('/api/services/downloadpdf/' + scope.invoice.id, { responseType: 'arraybuffer' }).then(function (response) { 
    var file = new Blob([response.data], { type: 'application/pdf' }); 
    file.name = 'invoice.pdf'; 
    var fileUrl = URL.createObjectURL(file); 
    $window.open(fileUrl, '_blank'); 
}); 
0

你可以用下面的代碼

var fileName = response.headers('content-name'); 
var blob = new Blob([response.data], {type: 'application/pdf'}); 
var url = window.URL.createObjectURL(blob); 

linkElement.setAttribute('href', url); 
linkElement.setAttribute("download", fileName); 

var clickEvent = new MouseEvent("click", { 
     "view": window, 
     "bubbles": true, 
     "cancelable": false 
}); 
linkElement.dispatchEvent(clickEvent); 
嘗試