首页 > 解决方案 > 打字稿检索 ArrayBuffer 的数组

问题描述

对不起,如果这已经被问到了,但我找不到任何东西。

我有一个在 Kotlin 中制作的后端的休息电话,在那里我得到了一个 ByteArray 列表。但我不知道如何在 Angular/TypeScript 中处理这个问题。在以前的版本中,我只有一个 ArrayBuffer,函数是这样的:

downloadDoc(correlationId: string): any {
  const httpOptions = {
    'responseType'  : 'arraybuffer' as 'json'
  };

  return this.httpClient
    .get<ArrayBuffer>('/rest/someUrl/' + correlationId, httpOptions)
    .map(res => {
      if(res.byteLength > 0) {
        return new Blob([res], {type: "image/png"});
      } else {
        return new Blob(['Your document was not found'], {type: "text/plain"});
      }
    });
}

这仅适用于 ArrayBuffer,但不适用于 List。

我试过这个,但似乎没有渲染图像

downloadDoc(correlationId: string): Observable<Array<any>> {
  return this.httpClient
    .get<Array<any>>('/rest/someUrl/' + correlationId)
    .map(results =>
      results.map(result => new Blob([result], { type: "image/png" })
    )
  );
}

我想要做的是:

openDocument() {
  this.pngService.downloadDoc(correlationId).subscribe(results => {
    const tab = window.open("", '_blank');
    let data = "";
    results.forEach(function (value) {
      const fileURL = URL.createObjectURL(value);
      data = data.concat("<img src=\"" + fileURL +"\">")
    });
    tab.document.write(data);
  });
}

但生成的 URL 已损坏,不会在新选项卡中加载任何图像。

标签: angulartypescript

解决方案


推荐阅读