ng2-pdf-viewer Access-Control-Allow-Origin

car*_*ier 6 pdf angular

我正在为Angular使用ng2-pdf-viewer库,我正在尝试从基本URL显示PDF.问题是我收到以下错误:

XMLHttpRequest无法加载 https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf.请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:54800 ",因此没有允许访问.

这是我的代码:

.ts:

{ Name: "Angular2 tuto", Description: "An amazing Angular2 pdf !", Url: {url: "https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf", withCredentials: true} }
Run Code Online (Sandbox Code Playgroud)

.html:

<pdf-viewer [src]="document.Url" [page]="1" [original-size]="false" style="display: block;"></pdf-viewer>
Run Code Online (Sandbox Code Playgroud)

我尝试了没有"withCredentials"属性 - 没有改变..

如何从angular2中的链接显示任何pdf?(虽然工作在一个基本的iframe ......)

小智 -4

导入这些:-

import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';
Run Code Online (Sandbox Code Playgroud)

定义变量:-

Url : SafeUrl;
Run Code Online (Sandbox Code Playgroud)

在构造函数中使用它,如下所示-

constructor(private sanitizer: DomSanitizer) {}
Run Code Online (Sandbox Code Playgroud)

像这样指定 URL -

this.Url = this.sanitizer.bypassSecurityTrustResourceUrl("https://www.bbv.ch/images/bbv/pdf/Publikationen/BBV16Poster_Angular2.pdf");
Run Code Online (Sandbox Code Playgroud)