Download file using java script and spring boot











up vote
1
down vote

favorite












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










share|improve this question
























  • 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 at 22:19










  • well i expect file to download when i click that button
    – user3364181
    Nov 11 at 8:34










  • I'm aware of that, but that's not what I was asking.
    – Joe C
    Nov 11 at 9: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 at 9: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 at 9:51















up vote
1
down vote

favorite












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










share|improve this question
























  • 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 at 22:19










  • well i expect file to download when i click that button
    – user3364181
    Nov 11 at 8:34










  • I'm aware of that, but that's not what I was asking.
    – Joe C
    Nov 11 at 9: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 at 9: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 at 9:51













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question















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







javascript java spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 15:30

























asked Nov 10 at 22:16









user3364181

931213




931213












  • 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 at 22:19










  • well i expect file to download when i click that button
    – user3364181
    Nov 11 at 8:34










  • I'm aware of that, but that's not what I was asking.
    – Joe C
    Nov 11 at 9: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 at 9: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 at 9:51


















  • 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 at 22:19










  • well i expect file to download when i click that button
    – user3364181
    Nov 11 at 8:34










  • I'm aware of that, but that's not what I was asking.
    – Joe C
    Nov 11 at 9: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 at 9: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 at 9:51
















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 at 22:19




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 at 22:19












well i expect file to download when i click that button
– user3364181
Nov 11 at 8:34




well i expect file to download when i click that button
– user3364181
Nov 11 at 8:34












I'm aware of that, but that's not what I was asking.
– Joe C
Nov 11 at 9:05




I'm aware of that, but that's not what I was asking.
– Joe C
Nov 11 at 9: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 at 9:19




i don't know, that's why i posted FE code and BE code and asking for help what to do
– user3364181
Nov 11 at 9: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 at 9:51




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 at 9:51












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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






share|improve this answer





















  • I managed to download pdf now, but its blank. It get pages and size but its blank
    – user3364181
    Nov 11 at 16:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243976%2fdownload-file-using-java-script-and-spring-boot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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






share|improve this answer





















  • I managed to download pdf now, but its blank. It get pages and size but its blank
    – user3364181
    Nov 11 at 16:22















up vote
0
down vote













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






share|improve this answer





















  • I managed to download pdf now, but its blank. It get pages and size but its blank
    – user3364181
    Nov 11 at 16:22













up vote
0
down vote










up vote
0
down vote









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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 15:51









Harry Smart

479




479












  • I managed to download pdf now, but its blank. It get pages and size but its blank
    – user3364181
    Nov 11 at 16:22


















  • I managed to download pdf now, but its blank. It get pages and size but its blank
    – user3364181
    Nov 11 at 16:22
















I managed to download pdf now, but its blank. It get pages and size but its blank
– user3364181
Nov 11 at 16:22




I managed to download pdf now, but its blank. It get pages and size but its blank
– user3364181
Nov 11 at 16:22


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243976%2fdownload-file-using-java-script-and-spring-boot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation