最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to download a PDF to a new tabwindow without popup blocker? - Stack Overflow

programmeradmin0浏览0评论

I have the following service call to download files from the server. I currently have it so that PDFs will open up in a new tab/window, and any other document types will be downloaded.

The problem I'm having right now is that the PDF is being prevented by a pop up blocker. Is there any way around this?

  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);
        });

I have the following service call to download files from the server. I currently have it so that PDFs will open up in a new tab/window, and any other document types will be downloaded.

The problem I'm having right now is that the PDF is being prevented by a pop up blocker. Is there any way around this?

  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);
        });
Share Improve this question asked Sep 9, 2016 at 16:37 JoeyJoey 5841 gold badge6 silver badges23 bronze badges 1
  • 1 You are forcing the click event, therefore popup blockers will suggest that this is not a user performed action. Have the user do the click action that opens the window and test again. – Mike B Commented Sep 9, 2016 at 16:41
Add a comment  | 

1 Answer 1

Reset to default 16

I found an answer to my question via another stack overflow post.

window.open popup getting blocked during click event

Basically, i call var newWindow = window.open(); before I make the service call and then newWindow.location = fileUrl in the success callback.

发布评论

评论列表(0)

  1. 暂无评论