Angular 7 download PDF file from POST

dataService.ts

downloadNoteReceipt(notes_purchased_id: number):Observable{
return this.httpClient.get(this.baseUrl + receipt/notespurchasedreceipt/ + notes_purchased_id, { responseType: “blob” } );
}
component.ts

download(booking_id: number) {
this.orderDetailsService.downloadNoteReceipt(booking_id).subscribe(res => {
console.log(res);
var newBlob = new Blob([res], { type: “application/pdf” });

  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(newBlob);
    return;
}
 // For other browsers: 
        // Create a link pointing to the ObjectURL containing the blob.
        const data = window.URL.createObjectURL(newBlob);

        var link = document.createElement('a');
        link.href = data;
        link.download = "receipt.pdf";
        // this is necessary as link.click() does not work on the latest firefox
        link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));

        setTimeout(function () {
            // For Firefox it is necessary to delay revoking the ObjectURL
            window.URL.revokeObjectURL(data);
        }, 100);

}, error => {
  console.log(error);
})

}
component.html

<i class=“fa fa-download” style=“font-size:20px;color:purple” aria-hidden=“true” (click)=“download(row.booking_id)”>
sendPost: void {
const options = {headers: {‘Content-Type’: ‘application/json’}};
this.http.post(‘http://localhost:8080/export’, JSON.stringify(this.filter),
options )
.subscribe((next) => console.log(next));
}

const a = document.createElement(‘a’);
document.body.appendChild(a);
a.style.display = ‘none’;

const blob = new Blob(next, {type: “octet/stream”}),
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = name;
a.click();
window.URL.revokeObjectURL(url);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值