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
javascript java spring-boot
|
show 2 more comments
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
javascript java spring-boot
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
|
show 2 more comments
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
javascript java spring-boot
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
javascript java spring-boot
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
|
show 2 more comments
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
|
show 2 more comments
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
I managed to download pdf now, but its blank. It get pages and size but its blank
– user3364181
Nov 11 at 16:22
add a comment |
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
I managed to download pdf now, but its blank. It get pages and size but its blank
– user3364181
Nov 11 at 16:22
add a comment |
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
I managed to download pdf now, but its blank. It get pages and size but its blank
– user3364181
Nov 11 at 16:22
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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