2015-06-02 46 views
0

我在服务器端使用jax-ws,在客户端使用angularjs。 我试图从服务器端下载一个xls文件发送。 在服务器端我用这个代码:下载xls文件angular jax-rs

@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(); 
} 

,我有以下的角码,以接收来自服务器端sended文件:

我的HTML代码:

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

我的控制器:

$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); 
}); 

};

和我的服务代码:

.....

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

问题是,当我为了下载中心XLS文件我得到一个xls文件没有得到很好的格式化的按钮点击。

响应varibale有一个错误值,像这样:

ࡱࡱࡱࡱࡱࡱࡱࡱ ࡱ ࡱ `“”@ @ ......

请注意,当我在浏览器中直接使用url“http://localhost:8090/myapp/rest-ws/team/exportemployeexls”时,我得到了一个很好的xls格式。

任何人都有一个想法如何解决这个问题,并使用angular和jax-rs下载一个xls文件? };

回答

1

我通过使用以下解决方案解决了问题。

在我的服务,我返回,允许从服务器

侧得到XLS的URL。

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

,并在我的控制器我打电话给我的服务,我使用window.open JavaScript函数:

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

终于在我的下载中心按钮CALS actionEmployeeXls点击时,我让我的xls文件。