首页 文章

在新的Chrome标签页中打开PDF Blob(Angular 2)

提问于
浏览
2

我正在加载PDF如下(我使用Angular 2,但我不确定这是否重要......):

//Inside a service class
downloadPdf = (id): Observable<Blob> => {
    let headers = new Headers();
    headers.append("Accept", "application/pdf");
    return this.AuthHttp.get(this.pdfURL + id, {
        headers: headers,
        responseType: ResponseContentType.Blob
    }).map(res => new Blob([res.blob()], {type: "application/pdf"}));
}

//Inside a click handler
this.pdfService.downloadPdf(this.id).subscribe((data: Blob) => {
        let fileURL = window.URL.createObjectURL(data);
        window.open(fileURL);
    });

这段代码在Firefox中运行良好 . 在Chrome中,新标签会暂时闪烁并关闭 . 当我调试并手动将冲浪放到对象URL时,Chrome可以很好地打开它 .

我在这做错了什么?

亲切的问候,

Ewout .

2 回答

  • 15

    广告拦截器阻止了新标签的打开 .

  • 2

    它无法工作,新的弹出窗口将被浏览器阻止,因为它是从回调创建的,而不是trusted event,要使其工作,必须直接从点击处理程序调用,或者你必须在浏览器中禁用bloking弹出窗口 .

    如果ajax调用在不到一秒的时间内返回,Chrome将只允许它按照需要运行 . More there

相关问题