C# 如何从Angular 9中的字节数组中打开PDF?

C# 如何从Angular 9中的字节数组中打开PDF?,c#,arrays,angular,pdf,webapi,C#,Arrays,Angular,Pdf,Webapi,我有一个WebApi方法,它将字节数组返回到角度前端。 我尝试了所有的方法,从将其作为Blob读取,到将其作为arraybuffer读取,再到将其从字节数组转换为Blob,都没有效果。每次我遇到无法打开pdf错误时 组件中的示例代码: this.service.getPDF().subscribe((response)=>{ let file = new Blob([response], { type: 'application/pdf' }); va

我有一个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);

}
getPDF(){
constURL=`${this.serviceUrl}/pdf`;
常量httpOptions={
'responseType':'arraybuffer'作为'json'
//'responseType':'blob'为'json'//这也有效
};
返回this.http.get(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); const blob=new blob([new Uint8Array(bytearray)],{type:“application/pdf”}); const exportUrl=URL.createObjectURL(blob); 打开(导出URL); revokeObjectURL(exportUrl);