downloading xlsx file in angular 2 with blob
I was having the same problem as yours - fixed it by adding responseType: ResponseContentType.Blob
to my Get options. Check the code below:
public getReport(filters: ReportOptions) {
return this.http.get(this.url, {
headers: this.headers,
params: filters,
responseType: ResponseContentType.Blob
})
.toPromise()
.then(response => this.saveAsBlob(response))
.catch(error => this.handleError(error));
}
The saveAsBlob() is just a wrapper for the FileSaver.SaveAs():
private saveAsBlob(data: any) {
const blob = new Blob([data._body],
{ type: 'application/vnd.ms-excel' });
const file = new File([blob], 'report.xlsx',
{ type: 'application/vnd.ms-excel' });
FileSaver.saveAs(file);
}
you can use the below code :
var file = new Blob([data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
saveAs(file,"nameFile"+".xlsx");
deferred.resolve(data);
After nothing works. I changed my server to return the same byte array with the addition of:
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=deployment-definitions.xlsx");
At my client I deleted the download function and instead of the GET part i did:
window.open(this.restUrl, "_blank");
This is the only way I found it possible to save an xlsx file that is not corrupted.
If you have a answer about how to do it with blob please tell me :)