我对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 :(得分:1)
如果你有文件链接就这样,在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');
});
答案 1 :(得分: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);