如何解决如何从Angular 9中的字节数组中打开PDF?
我有一个WebApi方法,该方法将字节数组返回到有角前端。 我已经尝试了一切,从将其读取为Blob,将其读取为arraybuffer,将其从字节数组转换为Blob,都没有用。每次我收到无法打开的pdf错误。
组件中的示例代码:
this.service.getPDF().subscribe((response)=>{
let file = new Blob([response],{ type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
service.ts中的示例代码:
getPDF(){
const url = `${this.serviceUrl}/pdf`;
const httpOptions = {
'responseType' : 'arraybuffer' as 'json'
//'responseType' : 'blob' as 'json' //This also worked
};
return this.http.get<any>(url,httpOptions);
}
解决方法
Angular的HttpClient中可能存在问题,由于某种原因,该问题未正确读取您的响应作为数组缓冲区。尝试将其转换为Uint8Array之前,它可以为您提供适当的结构,以准备进行进一步处理:
尝试:
const blob = new Blob([new Uint8Array(byteArrays)],{ type: "application/pdf" }); const exportUrl = URL.createObjectURL(blob); window.open(exportUrl); URL.revokeObjectURL(exportUrl);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。