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