I'm trying to download an xls file with my angular application, this has worked before angular 8 and suddenly doesn't work anymore. Here is what I have so far:
export(selectedUsers: any) {
this.getReport(selectedUsers).subscribe(data => this.downloadFile(data));
}
private getReport(selectedUsers: number[]): Observable<Blob> {
const headers = new HttpHeaders({'content-type' : 'application/json',
'Access-Control-Allow-Origin' : '*',
responseType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
return this.http.post<Blob>(this.globals.api + 'persoon/export', selectedUsers, {headers, 'blob'});
}
private downloadFile(data: Blob) {
console.log('downloading...');
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const url = window.URL.createObjectURL(blob);
window.open(url);
}
In the angular documentation it mentioned something like HttpEvent<Blob>, I also tried this but it didn't work. I tried out this similar question: HttpClient - How to request non-JSON data But the solution did not work either since it basically said that it didn't accept the responseType in the given position.
I also checked the response headers of my request, the content-type is also application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Here are my imports:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Mail } from './models/mail';
import { Klant } from './models/klant';
import { GlobalsService } from './globals.service';
import { Observable } from 'rxjs';
Can anyone point me in the right direction and help me solve this issue?
I got another issue after applying the accepted solution:
the expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
For this you should go here: Types of property 'responseType' are incompatible

{ responseType: 'text' }? I'm not sure this will work, but I'm not sure it won't either