Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file Safari

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?

like image 640
vlio20 Avatar asked Feb 27 '17 18:02

vlio20


1 Answers

Try without @ResponseBody and with produces = MediaType.APPLICATION_JSON_UTF8_VALUE

like image 124
NikNik Avatar answered Oct 16 '22 04:10

NikNik