2
votes

I use angularjs 1.3.14

I have a java REST services produce a xml file with this header: "Content-Disposition", "attachment; filename=yourFileName";

I need take the file with the name of my file on AngularJS.

I have this code:

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

but in myHeaders have only {content-type="application/xml"}. I need find "Content-Disposition", "attachment; filename=yourFileName"

Java Services:

@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
You should set the "Content-Disposition" header on your response. Can you post some of your java code ?Jib'z
try this response.header("Content-Disposition", "inline; filename=myFile.xml");Jib'z
I change "attachment; by "inline; but do not work againStéphane GRILLON
You can use tools like Fiddler (or anyone else) to see wether your response has the correct headers set. And then be more specific on your questions.Jib'z

2 Answers

2
votes

2 years after, I find a solution:

@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);
2
votes

This is a server side CORS issue. You need to enable this:

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