如何下载PDF到一个新的标签/窗口没有弹出阻止

How to download a PDF to a new tab/window without popup blocker?

本文关键字:窗口 标签 一个 下载 何下载 PDF      更新时间:2023-09-26

我有以下服务调用从服务器下载文件。我现在有这样的功能,pdf文件会在一个新的标签/窗口中打开,任何其他类型的文档都会被下载。

我现在遇到的问题是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);
        });

我在另一个堆栈溢出帖子中找到了我的问题的答案。

窗口。打开的弹出窗口被阻止在点击事件

基本上,我在进行业务呼叫之前调用var newWindow = window.open();,然后在成功回调时调用newWindow.location = fileUrl