Home » Javascript » Spring controller return file (works with csv, not with image type or zip)

Spring controller return file (works with csv, not with image type or zip)

Posted by: admin August 19, 2018 Leave a comment

Questions:

I have controller that works with returning file (stored as byte array).

@ResponseBody
@GetMapping(value = "/get")
public void getFile(HttpServletResponse response, @RequestParam(value = "id") String id) throws Exception {
    byte[] file = findFileById(id);

    response.reset();
    response.setBufferSize(BUFFER_SIZE);
    try {
        response.getOutputStream().write(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I make ajax get request in javascript and I make file new file with Blob(). It works fine with .csv files, but fails when I try to make it work for png, jpeg and zip files.

EDIT: I can make file in js, but image and zip files are corrupted.
Upon some inspection I found out that image files are larger than original.

Answers: