Dev Db
Dev Db

Reputation: 760

Angular 5 download excel file from web api ClosedXml

I am trying to download an excel file generated by my web api in my angular application. The application was first written in angularJS, and everything works fine over there. Now I am migrating the app to Angular 5 and it also works fine, but I get an error saying: type '"arraybuffer"' is not assignable to type '"json"'.

It uses HttpClient

this.http.post<any>(`${this.appConfig.api}/users`, users, {responseType: 'arraybuffer'})
  .subscribe(
    (next: ArrayBuffer) => {
      console.log(next);
      const file: Blob = new Blob([next], { type: 'application/xlsx' });
      saveAs(file, `Rapport users.xlsx`);
    }
  )

How can i get rid of the error?

Upvotes: 1

Views: 1866

Answers (2)

Sheila Mbadi
Sheila Mbadi

Reputation: 116

Try this out from the angular documentation. Link

this.http.get(`${this.appConfig.api}/users`, {responseType: 'blob'})
      .subscribe(
        (data) => {
          console.log(data);
          const file: Blob = new Blob([data], { type: 'application/xlsx' });
          saveAs(file, `Rapport users.xlsx`);
        }
      )

Upvotes: 1

Dev Db
Dev Db

Reputation: 760

Changed {responseType: 'arraybuffer'} to {responseType: 'arraybuffer' as 'json'}

Upvotes: 1

Related Questions