0

Im trying to return a File as a response from the server in order to be download from the client.

Im not sure what am i doing wrong i followed some examples but i still cant download the file, please assume that the file is already stored in the directory.

Here is my code:

@RequestMapping(value="/reports/downloadCsv", method=RequestMethod.POST)
        public void saveToCsv( 
            @Validated HttpServletRequest request,
            @Validated  HttpSession session,
            @Validated HttpServletResponse response, 
            @RequestParam String report) { 
InputStream in = new FileInputStream("C:\\server\\reports\\AccessReport.csv");
        response.setContentType("text/csv;charset=utf-8"); 
        response.setHeader("Content-disposition", "attachment; filename=AccessReport.csv");
        FileCopyUtils.copy(in, response.getOutputStream());
        response.flushBuffer();

}

And this is my ajax call:

$.ajax({
      url: "/server/reports/downloadCsv",
      type: "post",
      data: { 
          report: "AccessReport.csv"
          },
      type: 'POST',
      success: function(res){
      }
    });
Victor Elizondo
  • 275
  • 1
  • 4
  • 20
  • Do you have an error log and/or a stacktrace? – blgt Jun 19 '14 at 17:10
  • there are no exceptions throwed in my code neither in the js the problem is that the file is not being downloaded from the browser – Victor Elizondo Jun 19 '14 at 17:11
  • im not sure what im doing wrong or what am i missing, but the file is present in the absolute path. – Victor Elizondo Jun 19 '14 at 17:14
  • This might help you. http://stackoverflow.com/questions/20860269/i-can-not-download-file-via-ajax-in-spring-mvc-without-save-file-in-server http://stackoverflow.com/questions/13519058/download-file-with-ajax-post-request-via-spring-mvc – Ankur Singhal Jun 19 '14 at 17:43
  • 1
    Why are you handling the response with ajax? Does it work when you access the URL directly in the browser? Why not use `window.open`? Also using a POST instead of a GET is counter intuitive. You should use a GET request when getting resources. – Bart Jun 19 '14 at 20:49

0 Answers0