0

I use jax-ws in the server side and angularjs in the client side. I'm trying to download an xls file send from server side. In the server side i use this code:

 @GET
        @Path("/exportemployeexls")
        @Produces("application/vnd.ms-excel")
        @PermitAll
        public Response exportAllEmployeeOnXLSFile(){
         .......
         File employeeFile = new File("C://employee.xls");
         ResponseBuilder response = Response.ok((Object) employeeFile);
            response.header("Content-Disposition",
                    "attachment; filename=employeeFile.xls");
        return response.build();
}

And i have the following angular code in order to receive the file sended from the server side:

My html code:

<a href ng-click="actionTsunamiXls()" style="font-size: 80%;">
<img style="max-width:15px;" src="/img/csv.jpg"/>Tsunami Xls
</a>

My controller:

$scope.actionTsunamiXls = function () {
 console.info("Export XLS");
 NavService.getTsunamiXls(function (response) {
 $timeout(function () {
 var hiddenElement = document.createElement('a');
            hiddenElement.href = 'data:attachment/xls,' + encodeURIComponent(response);
            hiddenElement.download = 'result.xls';
            hiddenElement.click();
 }, 200);
 });

};

And My service code:

.....

service.getTsunamiXls = function (callback) {
 $http.get(url + ':' + port + '/' + context + '/rest-ws/team/exportemployeexls')
 .success(function (response) {
 callback(response);
 });

The problem is when i click on button in order to dowload the xls file i get an xls file not well formated.

The response varibale has a wrong value like this :

��ࡱ���ࡱ���ࡱ���ࡱ���ࡱ���ࡱ���ࡱ���ࡱ���ࡱ���ࡱ�`""@ @ ......

Note that when i use the url "http://localhost:8090/myapp/rest-ws/team/exportemployeexls" directely in the browser i get a well formated xls.

Anyone have an idea in how to solde this roblem and to dowload an xls file using angular and jax-rs ?? };

Yahia Ammar
  • 280
  • 4
  • 20

1 Answers1

1

i solved the problem by using the following solution.

In my service i return the url that allows to get the xls from the server

side.

service.getTsunamiXls = function () {
            return url + ':' + port + '/' + context + '/rest-ws/team/exportemployeexls'
        };

and in my controller i call my service and i use the window.open javascript function:

$scope.actionEmployeeXls = function () {
        console.info("Export XLS");
        $window.open(NavService.getTsunamiXls());
    };

and finally when clicking on my dowload button that cals actionEmployeeXls i get my xls file.

Yahia Ammar
  • 280
  • 4
  • 20