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);