我使用angularjs 1.3.14
我有一个Java REST服务产生带有以下标头的xml文件:“ Content-Disposition”,“ attachment; filename = yourFileName”;
我需要使用AngularJS上的文件名作为文件。
我有以下代码:
$http.get('/someUrl').success(function(data, status, headers){
var myHeaders = headers();
...
});
但在myHeaders中只有{content-type =“ application / xml”}。我需要找到“ Content-Disposition”,“附件; filename = 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年后,我找到了解决方案:
@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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句