angular - How to properly download excel file with Angular2 -


this code of service makes post request response xls file :

exportinternalorder(body) {         let user_token: string = this.sessionservice.gettoken();         let headers = new headers();         headers.append('responsetype', 'arraybuffer');         headers.append('authorization', 'bearer ' + user_token);         return this.http.post(this.config.exportinternalorder, body,{             headers: headers         }).map(res => new blob([res._body],{ type: 'application/vnd.ms-excel' }));     } 

which supposed handle response of excel file. code invoking it:

let objtosend = this.makeobjtosend(false);              this.reportingservice.exportexcel(objtosend)             .subscribe(                 data => {                     this.exportdata(data);                 },                 error => {                     this.errorfiltermsg.push({ severity: 'error', detail: 'report exporting has failed!' });                 }             ); 

and saving of file (for reason window.open nothing):

exportdata(data){                let blob = data;         let = document.createelement("a");         a.href = url.createobjecturl(blob);         a.download = 'filename.xls';         document.body.appendchild(a);         a.click();             } 

but file still saves corrupted one. while using postman , curl comes ok. appreciated.

responsetype shouldn't set in headers, it's part of requestoptionsargs object passed second argument in post function , requestoptionsargs contains headers, responsetype , others, can read more here. so, code should this:

import { responsecontenttype } '@angular/http';  exportinternalorder(body) {     let user_token: string = this.sessionservice.gettoken();     let headers = new headers();     headers.append('authorization', 'bearer ' + user_token);     return this.http.post(this.config.exportinternalorder, body,{         headers: headers,         responsetype: responsecontenttype.blob     }).map(res => new blob([res._body],{ type: 'application/vnd.ms-excel' })); } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -