I am trying to trigger file download, and I am having issues to do this on Safari (FireFox & Chrome works as expected).
Here is my Java code:
@RequestMapping(method = RequestMethod.GET, produces = "text/csv")
@ResponseBody
public String getReports(
final HttpServletResponse response,
final @RequestParam String data) throws ParseException {
response.setHeader("Content-type", "text/csv; charset=utf-8");
response.setHeader("Content-Disposition","attachment; filename='view.csv'");
String csv = exportCurrentService.extractMatrix(data);
response.setContentLength(csv.length());
return csv;
}
And here is my client code:
downloadURI(url, name) {
const a = document.createElement('a');
a.href = url;
a.download = name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
In Safari the response is printed out on the screen (loaded on the same page).
Note: that I have tried other methods suggested on SO but each has it's own problems.
Update: In the response header I see that the Content-Disposition is set to inline instead of attachment. Why this is happening?
What did I do wrong?
Try without @ResponseBody
and with produces = MediaType.APPLICATION_JSON_UTF8_VALUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With