在新标签的angular 4中打开PDF文件吗? [英] Opening PDF file in new tab angular 4?

查看:79
本文介绍了在新标签的angular 4中打开PDF文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 window.open()打开PDF文件,但是窗口会自动打开和关闭,并且该文件的下载方式与其他文件一样.如何使pdf文件在新标签页中打开?没有安装广告拦截器.

I tried opening a PDF file using the window.open(), but the window opens and closes automatically and the file is downloaded like any other file. How to make the pdf file open in new tab? There are no ad blockers installed.

推荐答案

从@barbsan的想法出发,我更改了http标头并接收到一个blob,并使用window.open()将其显示为pdf.奏效了.

From @barbsan idea, I changed the http headers and received a blob and used that to display the blob as pdf using window.open(). It worked.

这是我的示例代码.

在服务文件中

downloadPDF(url): any {
    const options = { responseType: ResponseContentType.Blob  };
    return this.http.get(url, options).map(
    (res) => {
        return new Blob([res.blob()], { type: 'application/pdf' });
    });
  }

在组件文件中

this.dataService.downloadPDF(url).subscribe(res => {
  const fileURL = URL.createObjectURL(res);
  window.open(fileURL, '_blank');
});

这篇关于在新标签的angular 4中打开PDF文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆