angularjs - How to download Excel file using Angular js and REST Api -
angular js code:
$scope.getexceldownload = function(index,basketid,brokerid,tradedate,serverid,transid) { var myformid = 'excelform' + index; exceldownload.get({brokerid : brokerid,basketname : basketid,appl : 'abc',param : 'excel',tradedate:tradedate,transactionid:transid,serverid:serverid},function(data, headers){ alert("success"); var response = {}; response.data = data; response.headers = headers(); var blob = new blob([response.data], {type: "application/octet-stream;base64"}); var objecturl = url.createobjecturl(blob); window.open(objecturl); saveas(blob, "myfile.xls"); },function(errresponse) { alert("failure"); }); }
rest webservice:
@requestmapping(value = "/getexcel/{brokerid}/{basketname}/{appl}/{param}/{tradedate}/{transactionid}/{serverid}", produces = { mediatype.application_octet_stream_value }, method = requestmethod.get) public @responsebody responseentity<byte[]> getfile(@pathvariable("brokerid") string brokerid,@pathvariable("basketname") string basketname,@pathvariable("appl") string appl,@pathvariable("param") string param,@pathvariable("tradedate") string tradedate,@pathvariable("transactionid") string transactionid,@pathvariable("serverid") string serverid){ responseentity respentity = null; ordersdownload orders = new ordersdownload(); if(param.equalsignorecase(excel_option)){ log.finest("exceldatacontroller@getfile selected option :" + param); byte[] reportbytes = orderdownloadservice.doget(brokerid, basketname, appl, param, tradedate, transactionid, serverid); httpheaders responseheaders = new httpheaders(); responseheaders.add("content-disposition", string.valueof(string .valueof((new stringbuffer("attachment; filename=\"")) .append( stringutils .replace(basketname, ":", "")) .append("_orders.xls\"")))); responseheaders.add("content-type",mediatype.application_octet_stream_value); //return reportbytes; respentity = new responseentity<byte[]>(reportbytes, responseheaders,httpstatus.ok); return respentity; } return null; }
response: file getting downloaded without contnet printing [object,object]
inside file.
this work me
// download excel template file $scope.downloadexceltemplate = function () { customerservice.downloadtemplate().then(function (response) { var linkelement = document.createelement('a'); try { var filename = response.headers('content-name'); var blob = new blob([response.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}); var url = window.url.createobjecturl(blob); linkelement.setattribute('href', url); linkelement.setattribute("download", filename); var clickevent = new mouseevent("click", { "view": window, "bubbles": true, "cancelable": false }); linkelement.dispatchevent(clickevent); } catch (ex) { } }, function (response) { toastr[messagetypes.error]('something went wrong.'); $scope.clearform(); $('#cl-modal-box').modal('hide'); }); }
this php function handles request
public function downloadtemplateaction() { $customer = new customer(); $filename = 'customer_import_template.xlsx'; $uploaddir = $customer->getdownloadrootdir(); $filepath = $uploaddir . '/' . $filename; // create phpexcel object read excel file $objphpexcel = \phpexcel_iofactory::load($filepath); // define writer object excel2007 || 2010 $objwriter = phpexcel_iofactory::createwriter($objphpexcel, "excel2007"); // set header params header('content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8'); // header('content-type: application/vnd.ms-excel; charset=utf-8'); header('content-disposition: attachment;filename=' . $filename); header('cache-control: max-age=0'); header('content-name: ' . $filename); $objwriter->save('php://output'); exit(); }
Comments
Post a Comment