简体   繁体   English

Angular 7 pdf 显示来自 blob 的文件

[英]Angular 7 pdf display file from blob

I'm returning a FileContentResult from my c# .NET API, in my angular 7 application I receive it as a blob with HttpClient and then use the following to open a new tab and try to display it. I'm returning a FileContentResult from my c# .NET API, in my angular 7 application I receive it as a blob with HttpClient and then use the following to open a new tab and try to display it.

this.myService.GetPdfBlob().subscribe(data => {
   var fileURL = window.URL.createObjectURL(data);
   window.open(fileURL, '_blank');
})

After doing this it opens up a new tab, but I see a huge json object's text where it starts with FileContents property and that value is a massive string that I imagine is the bytes of the pdf document.完成此操作后,它会打开一个新选项卡,但我看到一个巨大的 json 对象的文本,它以 FileContents 属性开头,该值是一个巨大的字符串,我想它是 pdf 文档的字节。 It looks kind of like this:它看起来像这样:

{"FileContents":"JVBERi0xLjUNJeLjz9MNCjI1IDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDg1NzU1L08gMjcvRSA3MzgzMy9OIDQvVCA4NTQxNS9IIFsgNDc5IDIyMl0+Pg1lbmRvYmoNICAgICAgICAgICAgICAgICAgDQozOCAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtb...it keeps going..", "FileDownloadName":"MyBigFile.pdf"}

How do I make it display the actual pdf document and not this big JSON object of my FileContentResult?如何让它显示实际的 pdf 文档,而不是我的 FileContentResult 的这个大 JSON object? I eventually need to display multiple file types, but I'm just starting with pdf.我最终需要显示多种文件类型,但我只是从 pdf 开始。

I changed up my C# code slightly to return an object that has a bytes array byte[] called Bytes and a filename.我稍微更改了我的 C# 代码以返回一个 object,它有一个名为 Bytes 的字节数组 byte[] 和一个文件名。 I still had the problem until I did this in my angular code:在我在 angular 代码中执行此操作之前,我仍然遇到问题:

this.http.get(myUrl).subscribe(response => {
   var byteCharacters = atob(response.Bytes);
   var byteNumbers = new Array(byteCharacters.length);
   for (var i = 0; i < byteCharacters.length; i++) {
      byteNumbers[i] = byteCharacters.charCodeAt(i);
   }
   var byteArray = new Uint8Array(byteNumbers);
   var blob = new Blob([byteArray], {type: "application/pdf"});
   var fileURL = URL.createObjectURL(blob);
   window.open(fileURL, '_blank');
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM