iframe src中的blob对象

问题描述 投票:-1回答:1

我正在尝试做一些看起来很简单的事情:我试图在iframe中显示一个window.URL.createObjectURL(* .pdf文件 - Blob)。我试图通过将iframe src属性绑定到window.URL.createObjectURL函数的输出来完成此操作。看我的代码:组件Html:

<iframe [src]="safeResourceUrl" class="iframe"></iframe>

绑定组件中的属性:

safeResourceUrl : SafeResourceUrl;
this.safeResourceUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);

网址似乎是这样的(示例):

“斑点:http://localhost:4200/84999abe-5a02-48e0-bf61-f693eb5e0f6b

这是它的内容(这不是错误,这些是HTML标签保存为PDF,但任何其他内容也应该没问题):

<html>
  <body>
     <label style="color:red;"> test file (HTML) </label>
  </body>
</html>

文件路径由Web Api get请求生成的Blob生成。请求配置为:

responseType: 'blob' 

而blob类型是

 type: 'application/octet-stream' 

我在这里错过了什么?正在使用正确的内容下载文件,但iframe内容(在开发人员工具中包含空HTML标记):

<html><head></head><body></body></html>

请支持

angular asp.net-web-api iframe httpresponse
1个回答
0
投票

经过几次测试后想出来:我将blob类型设置为'application / pdf'而不是用于下载的'application / octet-stream'。

const blob = new Blob([res.body], { type: obj.action == FileAction.save ?  'application/octet-stream' : 'application/pdf' }); // preview in a PDF format
© www.soinside.com 2019 - 2024. All rights reserved.