"内容处置" AngularJS

时间:2015-08-31 15:31:00

标签: java angularjs web-services rest

我使用angularjs 1.3.14

我有一个java REST服务生成一个带有这个标题的xml文件:" Content-Disposition"," attachment;文件名= yourFileName&#34 ;;

我需要在AngularJS上获取带有我文件名的文件。

我有这段代码:

$http.get('/someUrl').success(function(data, status, headers){
var myHeaders = headers();
...
});

但在myHeaders中只有{content-type =" application / xml"}。 我需要找到" Content-Disposition","附件;文件名= yourFileName"

Java服务:

@GET
@Path(EXPORT_URL)
@Produces(MediaType.APPLICATION_XML)
public Response export(@Context HttpServletRequest request) {
    String userName = request.getRemoteUser();
    if (userName != null) {
        ...
        ResponseBuilder response = Response.ok(myObject);
        response.header("Content-Disposition", "attachment; filename=myFile.xml");
        return response.build();
    } else {
        return Response.status(Status.FORBIDDEN).build();
    }
}

2 个答案:

答案 0 :(得分:2)

这是服务器端CORS问题。您需要启用此功能:

"Access-Control-Expose-Headers", "Content-Disposition"

答案 1 :(得分:0)

2年后,我找到了解决方案:

@RequestMapping(value = "/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML)
public ResponseEntity<String> export(...

HttpHeaders headers = new HttpHeaders();
headers.setAccessControlExposeHeaders(Collections.singletonList("Content-Disposition"));
headers.set("Content-Disposition", "attachment; filename=" + filename);
return new ResponseEntity<>(exportedContent, headers, HttpStatus.OK);