1

I have file.zip in DB like BLOB. I want create method in Spring controller for download this file on client side.

@RequestMapping(value = "/downloadResolution/{resolutionId}", method = RequestMethod.GET)
    public void downloadResolution(@PathVariable("resolutionId") Long resolutionId, HttpServletResponse response) {
        Resolution resolution = resolutionService.findOne(resolutionId);
        ResolutionArchive resolutionArchive = resolution.getResolutionArchive();
        if (resolutionArchive == null) return;
        byte[] archive = resolutionArchive.getArchive();
        //this byte[] archive - my zip file from db
    }

How can I change this methot In order to download this on client side?

User press download button. Methos get data from DB in byte[] and user can download it.

EDIT

I tried solution of @pleft and it work. and I knew - I use ajax for call method

function downloadResolution(resulutionId) {
        $.ajax({
            type: 'GET',
            dataType: "json",
            url: '/downloadResolution/' + resulutionId,
            success: function (data) {
            },
            error: function (xhr, str) {
            }
        });
    }

How realize this if I use ajax?

user5620472
  • 2,722
  • 8
  • 44
  • 97

1 Answers1

6

You can use the OutputStream of your HttpServletResponse to write your archive bytes there.

e.g.

response.setHeader("Content-Disposition", "attachment; filename=file.zip");
response.setHeader("Content-Type", "application/zip");
response.getOutputStream().write(archive);

EDIT

Sample download

@RequestMapping(value = "/downloadResolution/{resolutionId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void downloadResolution(@PathVariable("resolutionId") Long resolutionId, HttpServletResponse response) throws IOException {
    String test = "new string test bytes";
    response.setHeader("Content-Disposition", "attachment; filename=file.txt");
    response.getOutputStream().write(test.getBytes());
}
pleft
  • 7,567
  • 2
  • 21
  • 45
  • Not shows window for save file when I press "download" and downloadResolution method been executed – user5620472 Nov 11 '16 at 08:52
  • Are you sure your `resolutionArchive` is not null? Because in your code I see `if (resolutionArchive == null) return;` hence nothing will happen if your program returns. Can you debug it? – pleft Nov 11 '16 at 08:54
  • Yes I am sure. It is NOT NULL. – user5620472 Nov 11 '16 at 09:06
  • Ok, I have updated my answer with a very trivial code for downloading as a text file a String. I have tested it and it works for me (spring-boot). If it doesnt work in your project then something other might be wrong. – pleft Nov 11 '16 at 09:10
  • Have you tried with another browser? IE, Firefox, Chrome? I tested my sample with all browsers and it works. Also did you check your logs for any exception? – pleft Nov 11 '16 at 09:20
  • ooo mayby I call method from ajax? – user5620472 Nov 11 '16 at 09:51
  • I dont think you can download a file via AJAX normally. You have to do some tricks, please read here: http://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request BUT this is something completely different from your initial question. – pleft Nov 11 '16 at 10:18
  • I have a similar issue and i can download files on client side from spring controller. However, I also want to return a map (of records and other info) in the same api call. – saran3h Mar 20 '18 at 06:20