如何从Angular 11中的Bytearray下载PDF?

发布于 2025-01-29 03:43:23 字数 1817 浏览 5 评论 0原文

我有字节数组,我想下载没有任何库的PDF,例如文件放宽。

service.ts

    return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
         { responseType: "arraybuffer", observe: "response", params }
    );

component.ts

file(byte) {
    var byteArray = new Uint8Array(byte);
    var a = window.document.createElement('a');

    a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/json' }));
    a.download = "data.txt";

    // Append anchor to body.
    document.body.appendChild(a)
    a.click();


    // Remove anchor from body
    document.body.removeChild(a)
  }

//   download   function
    this.invoiceService.generateInvoice(id).pipe(
      finalize(() => this.loadingFlag = false)
    ).subscribe(
      resp => {
        console.log(resp);
         this.file(resp.body)
}

“在此处输入图像说明” ”

如果我编写此版本,则resp是:

如果我仪式:

 return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
  { responseType: "arraybuffer", observe: "response", params }
);

revs是

I have byte array and I want to download pdf without any library, for example file-saver.

service.ts

    return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
         { responseType: "arraybuffer", observe: "response", params }
    );

component.ts

file(byte) {
    var byteArray = new Uint8Array(byte);
    var a = window.document.createElement('a');

    a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/json' }));
    a.download = "data.txt";

    // Append anchor to body.
    document.body.appendChild(a)
    a.click();


    // Remove anchor from body
    document.body.removeChild(a)
  }

//   download   function
    this.invoiceService.generateInvoice(id).pipe(
      finalize(() => this.loadingFlag = false)
    ).subscribe(
      resp => {
        console.log(resp);
         this.file(resp.body)
}

enter image description hereenter image description here

If I write this version resp is :enter image description here

and if I rite:

 return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
  { responseType: "arraybuffer", observe: "response", params }
);

resp is
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2025-02-05 03:43:24

解决方案:在服务文件中,无需响应类型。

  downloadPDF(str) {
    const linkSource = 'data:application/pdf;base64,' + str;
    const downloadLink = document.createElement("a");
    const fileName = "sample.pdf";

    downloadLink.href = linkSource;
    downloadLink.download = fileName;
    downloadLink.click();
  }

Solution: In the service file there is no need of responseType.

  downloadPDF(str) {
    const linkSource = 'data:application/pdf;base64,' + str;
    const downloadLink = document.createElement("a");
    const fileName = "sample.pdf";

    downloadLink.href = linkSource;
    downloadLink.download = fileName;
    downloadLink.click();
  }
水染的天色ゝ 2025-02-05 03:43:24

尝试一下,它对我有用而没有文件放弃:

file(data: any) {
    const blob = new Blob([data], {type: 'application/pdf'});
      let url = URL.createObjectURL(blob);
      const pwa = window.open(url);
      if (!pwa || pwa.closed || typeof pwa.closed === 'undefined') {
        throw error( 'check if your browser block windows');
    }
 }

或者您可以使用此示例:

file(data: any) {
    const blob = new Blob([data], {type: 'application/pdf'});
      let filename = 'myPdfFile';
      let url= URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = fileName;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
}

浏览器上的第一个示例pdf ,第二个下载文件。

try this it's worked for me without file-saver:

file(data: any) {
    const blob = new Blob([data], {type: 'application/pdf'});
      let url = URL.createObjectURL(blob);
      const pwa = window.open(url);
      if (!pwa || pwa.closed || typeof pwa.closed === 'undefined') {
        throw error( 'check if your browser block windows');
    }
 }

or you can use this example:

file(data: any) {
    const blob = new Blob([data], {type: 'application/pdf'});
      let filename = 'myPdfFile';
      let url= URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = fileName;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
}

The first example open pdf on your browser and the second download the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文