Anus Malik
Anus Malik

Reputation: 1

how to download file using angular 7?

I am new to angular. Currently coding to download the file in angular. I do complete code but it shows me blob error. what I tried is shown in the code area kindly help me out.

 DownloadFile(filePath: string, fileType:string): Observable<any>{
  let fileExtension = fileType.trim();
  let input = filePath.replace(/\s/g, "");
  return this.http.post( this.rootURL+'/PaymentDetail' +input, '',
  { responseType: 'blob'}) 
    .map( 
      (res) => {
      var blob = new Blob([res.blob()], {type: fileExtension} )
       return blob;

    })
   }

This is the error which I am facing in service:

ERROR in src/app/shared/payment-detail.service.ts(47,32): error TS2339: Property 'blob' does not exist on type 'Blob'.

Upvotes: 0

Views: 76

Answers (1)

Dadasaheb Karande
Dadasaheb Karande

Reputation: 165

Try with below code. The difference is you are passing filepath and I am passing array from server side (returns FileResult from MVC). Not sure it will work in your case, but posting this may be it will helpful to you.

private DownloadFile(res: ArrayBuffer, fileName: string) { 
    var fileURL = URL.createObjectURL(new Blob([res], { type: "application/excel" }));
    var link = document.createElement("a");
    link.setAttribute("href", fileURL);
    link.setAttribute("download", fileName);
    link.style.display = "none";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

Upvotes: 1

Related Questions