【问题标题】:how to start downloading pdf in new tab and close that tab when download is over in angular如何在新标签中开始下载pdf并在下载结束时关闭该标签
【发布时间】:2021-03-24 06:35:51
【问题描述】:

我从服务器得到一个 base64 文件(pdf),我必须在新标签中打开并下载它,当下载结束时我必须关闭该标签,角度。我使用了这个,但它不起作用

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

【问题讨论】:

标签: javascript angular


【解决方案1】:

之前我遇到过这个问题,打开一个新标签后添加标签即可解决

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

【讨论】:

    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 2019-12-21
    • 2022-01-08
    • 1970-01-01
    • 2012-09-01
    • 2021-04-24
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多