2015-06-02 45 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文件。