Angular2 - export a file that is obtained from an api rest

0

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!

    
asked by Emiliano Torres 12.05.2017 в 21:56
source

1 answer

0

Well, after so much walking, I found the problem! ... I was here ... I was missing this line (the third, where does the response.blob ()):

public Download = (): Observable<Blob> => {
    return this._http.get(this.actionUrl + "sdownload", {responseType: ResponseContentType.Blob})
        .map((response: Response) => response.blob())
        .catch(this.handleError);
}

Thanks to those who made the comments!

    
answered by 22.05.2017 / 14:37
source