I have a button defined as:
<button pButton type="button" label="Download" data-icon="fa-cloud-download" (click)="download()"></button>
where the download
method delegates to a service, and the service call the api with a post method:
download(model:GlobalModel) {
let downloadURL = base + "rest/process/download";
let body = JSON.stringify(model);
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
this.http.post('http://localhost:48080/rest/process/download', body, options)
.toPromise()
.then(
response => {
console.log(response);
var mediaType = 'application/zip';
var blob = new Blob([response.blob()], {type: mediaType});
var filename = 'project.zip';
saveAs(blob, filename);//FileSaver.js libray
});
}
But by now the blob()
method has not been implemented and there are other answers using _body
but there is an typescript error like "_body is private".
The browser displays the download window, but when I download the file is corrupted and cannot open it (I check with postman
and the file is generated OK from the server).
How could I download a file properly?... if not possible, There is available a workaround?