V5NEXT
V5NEXT

Reputation: 2077

octet-stream on angular - file download not working

I am trying to download a file on a button click, api have been hit successfully am getting 200 response, but file is not being downloaded. It's working on postman.

My code is shown below

/****  click */

  downloadAsset() {
    this.detailsService.getCsvReport(this.jobId).subscribe(data => {});
  }


/**** Service */

getCsvReport(jobId): Observable<Object> {
    const header = { Accept: "application/octet-stream" };
    let endpoint: string = `${Endpoints.REPORT}jobs/${jobId}/filePath?`;
    return this.get(endpoint, header).pipe(
      map(report => {
        return report.data.parentXml;
      })
    );
  }
      <button
            class="btn btn-blue-border"
            style="float: right;"
            (click)="downloadAsset()"
          >
            Download Report
          </button>

Any changes or suggestion's would be appreciated.

Upvotes: 1

Views: 14256

Answers (2)

Yash Rami
Yash Rami

Reputation: 2317

You can try like this


getCsvReport(jobId): Observable<Object> {
    const header = { Accept: "application/octet-stream" };
    let endpoint: string = `${Endpoints.REPORT}jobs/${jobId}/filePath?`;
    return this.get(endpoint, header).pipe(
      map(report => {
      const a = document.createElement('a');
      document.body.appendChild(a);
      const blob: any = new Blob([report.data.parentXml], { type: 'octet/stream' });
      const url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = data.fileName;
      a.click();
      window.URL.revokeObjectURL(url);
      })
    );
  }

Upvotes: 4

AngularChan
AngularChan

Reputation: 155

I think you need to set the response type of your request.

An example with http.post:

this.http.post(url + "/getFile", params, { responseType: "blob" }).subscribe(...)

I hope i can help.

Upvotes: 0

Related Questions