简体   繁体   English

如何在新选项卡中开始下载 pdf 并在 angular 中下载结束时关闭该选项卡

[英]how to start downloading pdf in new tab and close that tab when download is over in angular

I get a base64 file(pdf) from server,which I have to open in new tab and download it,and when download is over I have to close that tab,in angular.I used this but its not working我从服务器得到一个 base64 文件(pdf),我必须在新选项卡中打开并下载它,下载结束后我必须关闭该选项卡,在 angular 中。我使用了这个,但它不起作用

this.http.get(`url`)
        .subscribe((response: any) => {
            const filePath = 'data:application/pdf;base64,' + response.data;
            const aTag: any = document.createElement('a');
            const fileName = response.name;
            aTag.href = filePath;
            aTag.download = fileName;
            document.body.appendChild(aTag);
            window.open(filePath, '_blank');
            aTag.click();
            document.body.removeChild(aTag);
            window.close();
        }, (error) => {
            this.notificationMsgService.errorHandler(error);
        })

Earlier I faced that issue,it will be solved by appending a tag after opening a new tab早些时候我遇到过这个问题,打开一个新标签后添加一个标签可以解决这个问题

this.http.get(`url`)
    .subscribe((response: any) => {
        const filePath = 'data:application/pdf;base64,' + response.data;
        const aTag: any = document.createElement('a');
        const fileName = response.name;
        aTag.href = filePath;
        aTag.download = fileName;
        window.open(filePath, '_blank');
        document.body.appendChild(aTag);
        aTag.click();
        document.body.removeChild(aTag);
        window.close();
    }, (error) => {
        this.notificationMsgService.errorHandler(error);
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM