我使用 angularjs 1.3.14
我有一个 java REST 服务生成一个带有以下 header 的 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”、“attachment; 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);
关于java - "Content-Disposition"和 AngularJS header 中的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32314827/