[英]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.
this.myService.GetPdfBlob().subscribe(data => {
var fileURL = window.URL.createObjectURL(data);
window.open(fileURL, '_blank');
})
完成此操作后,它会打开一个新选项卡,但我看到一个巨大的 json 对象的文本,它以 FileContents 属性开头,该值是一个巨大的字符串,我想它是 pdf 文档的字节。 它看起来像这样:
{"FileContents":"JVBERi0xLjUNJeLjz9MNCjI1IDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDg1NzU1L08gMjcvRSA3MzgzMy9OIDQvVCA4NTQxNS9IIFsgNDc5IDIyMl0+Pg1lbmRvYmoNICAgICAgICAgICAgICAgICAgDQozOCAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtb...it keeps going..", "FileDownloadName":"MyBigFile.pdf"}
如何让它显示实际的 pdf 文档,而不是我的 FileContentResult 的这个大 JSON object? 我最终需要显示多种文件类型,但我只是从 pdf 开始。
我稍微更改了我的 C# 代码以返回一个 object,它有一个名为 Bytes 的字节数组 byte[] 和一个文件名。 在我在 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.