If you want to download a file with Angular? The problem is that the observable runs in another context, so when you try to create the URL var, you have an empty object and not the blob you want.
1 2 3 | this._reportService.getReport().subscribe(data => this.downloadFile(data)), //console.log(data), error => console.log('Error downloading the file.'), () => console.info('OK'); |
When the request is ready it will call the function “downloadFile” that is defined as follows:
1 2 3 4 5 | downloadFile(data: Response) { const blob = new Blob([data], { type: 'text/csv' }); const url= window.URL.createObjectURL(blob); window.open(url); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.