AngularJS / JAX-RS : how to have a proper Excel doc response -
i export excel file via jax-rs webservice , download / open web application. sounds works whether called curl not called angularjs v1.4.7 front end. using microsoft excel 2013 64 bits under windows 7 professional.
on server side webservice implemented below:
@post @produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") @consumes(mediatype.multipart_form_data) @path("/compare") public response compare( @formdataparam("controlfile") inputstream controlis, @formdataparam("controlfile") formdatacontentdisposition controlfiledetail, @formdataparam("testfile") inputstream testis, @formdataparam("testfile") formdatacontentdisposition testfiledetail ) { //(...) //creating excel file using apache poi sxssfworkbook file file = new file(reportfilename); responsebuilder responsebuilder = response.ok((object) file, mediatype.application_octet_stream) .header("access-control-allow-origin", "*") .header("content-disposition", "attachment; filename=test.xlsx"); return responsebuilder.build(); }
i can query webservice via curl , obtain expected result
curl -x post -f "controlfile=@loremipsum1.xml" -f "testfile=@loremipsum2.xml" http://localhost:8080/xmlcompare-rs/xmlcompare/compare > comparison.xls
for information without redirection in file exported format looks like:
åì⌐h ♂ _rels/.rels╡Æ┴j├0♀å∩y ú{π┤â1f£^╩á╖1║╨l%1i,c{[·÷≤vzj[ c║ ²· ☼ñz;oú°á►-;♣δ▓☻an│▒«s≡zxz=Çê ¥┴æ↔)8räms╘/4b╩;▒╖>èlΓóé>% (e╘=m↑k÷Σ≥ñσ0a╩mΦñg=`grsu≈2ⁿ÷Ǫ►b,|┼▐{│å∩╤▓♫↑:j (...)
i working call exact same webservice angularjs front end wherein webservice call below:
angular.module('comparewwwapp') .service('compareservice', ['$http', function ($http) { this.xmlcompare = function(controlfile, testfile){ var fd = new formdata(); fd.append('controlfile', controlfile); fd.append('testfile', testfile); var xmlcompareurl='http://localhost:8080/xmlcompare-rs/xmlcompare/compare'; $http.post(xmlcompareurl, fd, { transformrequest: angular.identity, headers: {'content-type': undefined} }) .success(function(result){ var blob = new blob([result], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); var objecturl = url.createobjecturl(blob); window.open(objecturl); }) .error(function(){ }); } }]);
the webservice sounds executed server side expected (from logs) , returns xlsx document shown in browser.
clicking on downloaded doc, excel opens , tells me "we found problem content in xxx.xlsx. want try recover mych can?(..)" , "excel cannot open file xxx.xlsx because file format or file extension not valid. verify file has not been corrupted , file extension matches format"
i did few attempts switching type 'application/vnd.ms-excel' or returning streamingoutput instead of mediatype.application_octet_stream object depicted here without success.
any advice ?
thank you,
you need add responsetype: 'arraybuffer'
post call. ok.
so be
$http.post(xmlcompareurl, fd, { transformrequest: angular.identity, headers: {'content-type': undefined}, responsetype: 'arraybuffer' })
Comments
Post a Comment