title: Angular 5 download excel file with post request
body: <p>I am facing an issue where I have downloaded an Excel file with Angular 1 but if I am implementing the same code in Angular 5 it is showing the error that your file is corrupted. My response is in <strong>ArrayBuffer</strong> and I am unable to read the file.</p>
  
  <p>Below is my code: </p>
  
  <p><strong>Service:</strong></p>
  
  <pre><code> DownloadData(model:requiredParams):Observable&lt;any&gt;{
    const headers = new Headers();
    const requestOptions = new RequestOptions({ headers: headers });
    requestOptions.headers.append('Content-Type', 'application/json');
  
     const body = JSON.stringify(model);
    return this.http.post(url, body, requestOptions)
    .map((res:any) =&gt; res)
   .catch((e: any) =&gt; Observable.throw(this.errorHandler(e)));
   }
  </code></pre>
  
  <p><strong>Component:</strong></p>
  
  <pre><code>exportToExcel() {
      this.loadingOverlayFlag = true;
     this.podashboardService.DownloadData(this.data).subscribe(result=&gt;{
      console.log(result);
      this.downloadFile(result._body,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'export.xlsx');
    })
    }
  
   downloadFile(blob: any, type: string, filename: string) {
  
   var binaryData = [];
     binaryData.push(blob);
  
       const url = window.URL.createObjectURL(new Blob(binaryData, {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})); // &lt;-- work with blob directly
  
       // create hidden dom element (so it works in all browsers)
       const a = document.createElement('a');
       a.setAttribute('style', 'display:none;');
       document.body.appendChild(a);
  
       // create file, attach to hidden element and open hidden element
       a.href = url;
       a.download = filename;
       a.click();
  
     }
  </code></pre>
  
  <p>I am able to download the file, but unable to read its content. The error is:</p>
  
  <blockquote>
    <p>Microsoft Excel<br>
    Excel cannot open the file '███████ DASHBOARD (5).xlsx' because the file format or file extension is not valid.
    Verify that the file has not been corrupted and that the file extension matches the format of the file.  OK</p>
  </blockquote>