我正在开发一个需要显示 PDF 文件的 Angular 应用程序。为了实现这一点,我目前正在使用 [ng2-pdf-viewer][1] 组件。该组件适用于以下文件:
但是,我确实遇到了诸如以下文件的 CORS 问题:
我收到的错误消息:
无法加载 htt://frontendmasters.com/assets/resources/lukasruebbelke/better-apps-angular-2-day1.pdf:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'htt://localhost:4200'。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
core.es5.js:1020 错误错误:未捕获(承诺):DataCloneError:无法在“Worker”上执行“postMessage”:TypeError:无法克隆无法获取。错误:无法在“Worker”上执行“postMessage”:TypeError:无法克隆无法获取。
我的代码
组件.html
<pdf-viewer
[src]="document.url.url"
[page]="1"
[original-size]="false"
style="display: block;">
</pdf-viewer>
Run Code Online (Sandbox Code Playgroud)
组件.ts
document = {
name: 'Angular 2',
description: 'An amazing Angular 2 pdf',
url: {
url: 'https://frontendmasters.com/assets/resources/lukasruebbelke/better-apps-angular-2-day1.pdf',
withCredentials: true
}
}
Run Code Online (Sandbox Code Playgroud)
如果您希望我提供任何其他信息,请告诉我。
对于使用AWS S3的人来说,这个问题可以通过配置CORS来解决。
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
Run Code Online (Sandbox Code Playgroud)
有关更多信息 https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html
| 归档时间: |
|
| 查看次数: |
6147 次 |
| 最近记录: |