How to download pdf in Angular on same Tab?

  angular, html, javascript, typescript

I want to download pdf , so I have created a button and On button click i call function downloadIcs203Data();.

Currently on click, my pdf file is open in new tab but i want to download on same tab.

<button class="btn-style red-btn" type="button" (click)="downloadIcs203Data();"><span class="material-icons">file_download</span>Download PDF</button>

Function code

downloadIcs203Data(): any {
  this.helper.toggleSidebarVisibility(true);
  this.apiService.downloadIcs203({downloadPDF : 1}).subscribe(data => {
  

  location.href = data.data.icspath;

  this.helper.toggleSidebarVisibility(false);
    swal.fire(
      '',
      data.message,
      'success'
    );
  }, (err: any) => {
    this.helper.toggleSidebarVisibility(false);
    swal.fire(
      'Error',
      err.error.message,
      'error'
    );
  });
}

So is there any solution or suggestion for me to download pdf file without changes tab.

Source: Ask Javascript Questions

LEAVE A COMMENT