1

This is my javascript code :

$('#downloadTraining').on('click', function () {
    $.get(restbase() + "/download-training/" + $('[name=trainingId]').val()).done(function(data) {
            }).fail(function(data) {
            errorAlert();
        }).always(function(data) {
    });
});

And this is my spring boot controller method :

@GetMapping("/r/download-training/{trainingId}")
public ResponseEntity<InputStreamResource> download(@PathVariable("trainingId") Integer trainingId) throws FileNotFoundException, JRException, IOException{
    File file = jasperReportService.createTrainingReport(trainingId);
    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));  
    return ResponseEntity.ok()
                    // Content-Disposition
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
                    // Content-Type
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    // Contet-Length
                    .contentLength(file.length()) //
                    .body(resource);
}

Calling get method from javascript is working. Getting file is working. But it doesn't download file. Is there anything i need to add to javascript or error is in Java ?

Response and request headers : http://prntscr.com/lh01zx

user3364181
  • 531
  • 3
  • 14
  • 32
  • Are the HTTP response headers what you're expecting? (Developer Tools in your browser should be able to tell you this.) – Joe C Nov 10 '18 at 22:19
  • well i expect file to download when i click that button – user3364181 Nov 11 '18 at 08:34
  • I'm aware of that, but that's not what I was asking. – Joe C Nov 11 '18 at 09:05
  • i don't know, that's why i posted FE code and BE code and asking for help what to do – user3364181 Nov 11 '18 at 09:19
  • If you use Google Chrome, you can find this out by opening the Developer Tools (press F12), clicking on the Network tab, and refreshing the page. You can then see the headers that your browser is sending to your service, and what headers your service is returning to your browser. Once you have this information, please [edit] your question to include it. – Joe C Nov 11 '18 at 09:51
  • I edited my question with that – user3364181 Nov 11 '18 at 15:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183454/discussion-between-user3364181-and-joe-c). – user3364181 Nov 11 '18 at 18:01

1 Answers1

0

I have a solution but I don't know if this is what you are expecting. Firstly I have never heard of sprint-boot before so sorry about that. But when it comes to downloading files with JavaScript I always use this method. This method downloads files with a blob and directly from the browser.

code:

//saving and downloading a file with a js blob;
function downloadFile(data, filename, type) {
    var file = new Blob([data], {type: type});
    if (window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(file, filename); //IE 10+
    } else {
        var a = document.createElement("a");
        var url = window.URL.createObjectURL(file);
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        setTimeout(function() {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);
        }, 0);
    }
}

I got this code from this question: JavaScript: Create and save file

Hazmatron
  • 181
  • 2
  • 16