【问题标题】:How can I open a PDF from array of bytes in Angular 9?如何从 Angular 9 中的字节数组打开 PDF?
【发布时间】:2020-09-07 14:15:40
【问题描述】:

我有一个 WebApi 方法,它将一个字节数组返回到一个角度前端。 我已经尝试了一切,从将其读取为 Blob,将其读取为数组缓冲区,将其从字节数组转换为 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);

}

【问题讨论】:

    标签: c# arrays angular pdf webapi


    【解决方案1】:

    问题可能存在于 Angular 的 HttpClient 中,由于某种原因,它无法将您的响应正确读取为数组缓冲区。尝试将其转换为 Uint8Array 之前,它可以为您提供适合进一步处理的结构:

    试试看:

    const blob = new Blob([new Uint8Array(byteArrays)], { type: "application/pdf" }); 常量 exportUrl = URL.createObjectURL(blob); window.open(exportUrl); URL.revokeObjectURL(exportUrl);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-19
      • 2017-12-03
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多