I want to download a file from Web Api, using Angular as a client side. My file is unfortunately broken when I downloading it at my browser. I think it is a fault of type discrepancy.
Web Api controller returns "byte[]"
Angular service:
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });
return this.http.post(this.baseUrl + '/MyAction', ac, options)
.map(res => res.blob())
.catch(this.handleError);
Angular component ts
this.service.downloadXls(items).subscribe(
data => this.downloadFile(data)),
error => console.log("Error downloading the file."),
() => console.info("OK");
}
downloadFile(data: Blob) {
let blob = new Blob([data], { type: 'application/vnd.ms-excel' });
let url= window.URL.createObjectURL(blob);
window.open(url);
}
As I understand I need to receive byte[] and convert it to blob on the client side. Or convert and send blob on the server side. I don't know how to do either.