Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - download response as a file

I am writing application using AngularJS and Spring. I would like to send request to the server and download response returned from controller as a file. In controller I have content of csv file (as string) i.e. 1;2;3;4 (1 row, 4 columns). What is the simplest way to download this response as a file?

Below, I posted my simplified code. In Spring controller:

@RequestMapping(value = "/csv", method = GET) @ResponseBody public String getCsvFile() {     return getCsvContent(); } 

In javascript (AngularJS)

return $http({method: 'GET', url: 'csv/'}); 

I was trying to write to the response stream also (below), setting headers, but on client side I always get this content as a string - not as a file to download.

@RequestMapping(value = "/csv", method = GET) @ResponseBody public void getCsvFile(HttpServletResponse response) {     response.setContentType("application/csv");     response.setHeader("Content-Disposition", "attachment; filename=file.csv");     response.setContentLength(getCsvContent().getBytes().length);     ServletOutputStream out = response.getOutputStream();     out.write(getCsvContent());     out.flush();     out.close(); } 

Does anyone knows how to write controller's method correctly in order to download response as a file on client side?

like image 529
mchrobok Avatar asked Jun 18 '13 19:06

mchrobok


People also ask

How do I download files from spring boot?

Spring Boot File Download from Local File System It is a simple GET URL and on the click of that URL the file will be downloaded automatically in the browser as we will be adding Content-Disposition in the response header as an attachment and the content type as application/octet-stream.


2 Answers

You can't download a file through an XHR request (which is how Angular makes it's requests). See Why threre is no way to download file using ajax request? You either need to go to the URL via $window.open or do the iframe trick shown here: JavaScript/jQuery to download file via POST with JSON data

like image 162
dnc253 Avatar answered Oct 04 '22 13:10

dnc253


I've wrestled with this myself, trying to make it work from the server. Couldn't. Instead...

  1. To clarify on @dnc253's answer, $window.open(URL) is a method for having an Angular application open a given URL in another window. (It's really just a testable angular proxy for the universal window.open().) This is a great solution, preserves your history, and gets the file downloaded and possibly renders it in that fresh browser window if that's supported. But it often runs into popup blockers, which is a huge problem for reliability. Users often simply don't understand what's going on with them. So, if you don't mind immediately downloading the file with the current window, you can simply use the equally effective universal javascript method: location.href = "uriString", which works like a charm for me. Angular doesn't even realize anything has happened. I call it in a promise handler for once my POST/PUT operation has completed. If need be, have the POST/PUT return the URL to call, if you can't already infer it. You'll get the same behavior for the user as if it had downloaded in response to the PUT/POST. For example:

    $http.post(url, payload).then(function(returnData){     var uriString = parseReturn(returnData);     location.href="uriString" }) 
  2. You can, in fact, download something directly from an XHR request, but it requires full support for the HTML5 file API and is usually more trouble than it's worth unless you need to perform local transformations upon the file before you make it available to the user. (Sadly, I lack the time to provide details on that but there are other SO posts about using it.)

like image 21
XML Avatar answered Oct 04 '22 14:10

XML