如何从Angular 11中的Bytearray下载PDF?
我有字节数组,我想下载没有任何库的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)
}
如果我仪式:
return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
{ responseType: "arraybuffer", observe: "response", params }
);
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)
}
If I write this version resp is :
and if I rite:
return this.http.get(`${this.invoiceUrl}/GenerateInvoice`,
{ responseType: "arraybuffer", observe: "response", params }
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案:在服务文件中,无需响应类型。
Solution: In the service file there is no need of responseType.
尝试一下,它对我有用而没有文件放弃:
或者您可以使用此示例:
浏览器上的第一个示例
pdf
,第二个下载文件。try this it's worked for me without file-saver:
or you can use this example:
The first example open
pdf
on your browser and the second download the file.