如何在没有弹出窗口拦截器的情况下将 PDF 下载到新的 tab/window?
How to download a PDF to a new tab/window without popup blocker?
我有以下服务调用来从服务器下载文件。我目前拥有它,因此 PDF 将在新 tab/window 中打开,并且将下载任何其他文档类型。
我现在遇到的问题是弹出窗口阻止程序阻止了 PDF。有什么解决办法吗?
return formService.getForm(params)
.$promise
.then(response => {
var blob = new Blob([response.data], {
type: response.responseType
});
var fileUrl = (window.URL || window.webkitURL).createObjectURL(blob);
if (response.responseType === 'application/pdf') {
window.open(fileUrl);
} else {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none"
a.href = fileUrl;
a.download = formName;
a.target = "_blank";
a.click();
window.URL.revokeObjectURL(fileUrl);
}
})
.catch(error => {
console.error(`Error downloading form '${formName}' `, error);
});
我通过另一个堆栈溢出找到了问题的答案 post。
window.open popup getting blocked during click event
基本上,我在调用服务之前调用 var newWindow = window.open();
,然后在成功回调中调用 newWindow.location = fileUrl
。
我有以下服务调用来从服务器下载文件。我目前拥有它,因此 PDF 将在新 tab/window 中打开,并且将下载任何其他文档类型。
我现在遇到的问题是弹出窗口阻止程序阻止了 PDF。有什么解决办法吗?
return formService.getForm(params)
.$promise
.then(response => {
var blob = new Blob([response.data], {
type: response.responseType
});
var fileUrl = (window.URL || window.webkitURL).createObjectURL(blob);
if (response.responseType === 'application/pdf') {
window.open(fileUrl);
} else {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none"
a.href = fileUrl;
a.download = formName;
a.target = "_blank";
a.click();
window.URL.revokeObjectURL(fileUrl);
}
})
.catch(error => {
console.error(`Error downloading form '${formName}' `, error);
});
我通过另一个堆栈溢出找到了问题的答案 post。
window.open popup getting blocked during click event
基本上,我在调用服务之前调用 var newWindow = window.open();
,然后在成功回调中调用 newWindow.location = fileUrl
。