I have an api rest that generates a file and generates the download. This api, I call it from the browser and the file is downloaded and I can open it and it has the correct content. Now ... I need to call it from an angular2 app. My service is as follows:
public Download = (): Observable<Blob> => {
return this._http.get(this.actionUrl + "sdownload", {responseType: ResponseContentType.Blob})
.map((response: Response) => {
})
.catch(this.handleError);
}
In my component I have:
onExport(): void {
this.serviceUser
.Download()
.subscribe(data => {
this.downloadfile(data);
}),
error => {
console.log(error);
},
() => {
console.log('download usuarios finalizado');
};
}
downloadfile(data: any) {
var blob = new Blob([data], { type: 'application/octet-stream' });
console.log(blob);
var url= window.URL.createObjectURL(blob);
window.open(url);
}
When executing this code, a file that only contains inside is the word "Undefined". Can you give me a hand? ... Thanks in advance!