我在服务器端使用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文件? };