Spring get request file not being downloaded
I want to download a file when clicking on a button in my AngularJS app which runs on Tomcat with a Java Spring backend but nothing is happening. The method in the backend is called and everything seems to have worked....but my browser doesn't download anything.
What am I missing?
Here's the AngularJS code, which logs Export-Response:[object Object]
:
exportProjects() {
let filteredProjectIds = ;
for (let i in this.filteredProjects) {
for (let x = 0, l = this.filteredProjects[i].length; x < l; x++) {
if (!this.isOldProjectsBundle(this.filteredProjects[i][x])) {
filteredProjectIds.push(this.filteredProjects[i][x].id);
}
}
}
this.$http.get('/profiles/projectWordExport?filteredProjects=' + filteredProjectIds.join(",")).then(response => {
console.log("Export-Response:" + response);
return response;
});
}
This is the Java code being called (it's really being called, already debugged it, no errors occuring):
@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET)
public void getProjectsWord(HttpServletRequest request, HttpServletResponse response, @RequestParam String filteredProjects) throws Exception {
//Load project objects from input string or load all projects if input empty
List<Project> projects = new java.util.ArrayList<>();
if (filteredProjects.isEmpty()) {
projects = projectRepository.findAll();
} else {
String pIds = filteredProjects.split(",");
for (String pId : pIds) {
projects.add(projectRepository.findById(Long.parseLong(pId)));
}
}
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-disposition", "attachment;filename=Projektexport.docx");
try {
SaveToZipFile saver = new SaveToZipFile(printer.printProjects(this.prepareProjectExport(projects)));
saver.save(response.getOutputStream());
response.flushBuffer();
} catch (NullPointerException e) {
response.setStatus(500);
response.sendError(500, "Fehler beim exportieren des Tests aufgetreten");
}
}
angularjs spring download
add a comment |
I want to download a file when clicking on a button in my AngularJS app which runs on Tomcat with a Java Spring backend but nothing is happening. The method in the backend is called and everything seems to have worked....but my browser doesn't download anything.
What am I missing?
Here's the AngularJS code, which logs Export-Response:[object Object]
:
exportProjects() {
let filteredProjectIds = ;
for (let i in this.filteredProjects) {
for (let x = 0, l = this.filteredProjects[i].length; x < l; x++) {
if (!this.isOldProjectsBundle(this.filteredProjects[i][x])) {
filteredProjectIds.push(this.filteredProjects[i][x].id);
}
}
}
this.$http.get('/profiles/projectWordExport?filteredProjects=' + filteredProjectIds.join(",")).then(response => {
console.log("Export-Response:" + response);
return response;
});
}
This is the Java code being called (it's really being called, already debugged it, no errors occuring):
@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET)
public void getProjectsWord(HttpServletRequest request, HttpServletResponse response, @RequestParam String filteredProjects) throws Exception {
//Load project objects from input string or load all projects if input empty
List<Project> projects = new java.util.ArrayList<>();
if (filteredProjects.isEmpty()) {
projects = projectRepository.findAll();
} else {
String pIds = filteredProjects.split(",");
for (String pId : pIds) {
projects.add(projectRepository.findById(Long.parseLong(pId)));
}
}
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-disposition", "attachment;filename=Projektexport.docx");
try {
SaveToZipFile saver = new SaveToZipFile(printer.printProjects(this.prepareProjectExport(projects)));
saver.save(response.getOutputStream());
response.flushBuffer();
} catch (NullPointerException e) {
response.setStatus(500);
response.sendError(500, "Fehler beim exportieren des Tests aufgetreten");
}
}
angularjs spring download
add a comment |
I want to download a file when clicking on a button in my AngularJS app which runs on Tomcat with a Java Spring backend but nothing is happening. The method in the backend is called and everything seems to have worked....but my browser doesn't download anything.
What am I missing?
Here's the AngularJS code, which logs Export-Response:[object Object]
:
exportProjects() {
let filteredProjectIds = ;
for (let i in this.filteredProjects) {
for (let x = 0, l = this.filteredProjects[i].length; x < l; x++) {
if (!this.isOldProjectsBundle(this.filteredProjects[i][x])) {
filteredProjectIds.push(this.filteredProjects[i][x].id);
}
}
}
this.$http.get('/profiles/projectWordExport?filteredProjects=' + filteredProjectIds.join(",")).then(response => {
console.log("Export-Response:" + response);
return response;
});
}
This is the Java code being called (it's really being called, already debugged it, no errors occuring):
@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET)
public void getProjectsWord(HttpServletRequest request, HttpServletResponse response, @RequestParam String filteredProjects) throws Exception {
//Load project objects from input string or load all projects if input empty
List<Project> projects = new java.util.ArrayList<>();
if (filteredProjects.isEmpty()) {
projects = projectRepository.findAll();
} else {
String pIds = filteredProjects.split(",");
for (String pId : pIds) {
projects.add(projectRepository.findById(Long.parseLong(pId)));
}
}
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-disposition", "attachment;filename=Projektexport.docx");
try {
SaveToZipFile saver = new SaveToZipFile(printer.printProjects(this.prepareProjectExport(projects)));
saver.save(response.getOutputStream());
response.flushBuffer();
} catch (NullPointerException e) {
response.setStatus(500);
response.sendError(500, "Fehler beim exportieren des Tests aufgetreten");
}
}
angularjs spring download
I want to download a file when clicking on a button in my AngularJS app which runs on Tomcat with a Java Spring backend but nothing is happening. The method in the backend is called and everything seems to have worked....but my browser doesn't download anything.
What am I missing?
Here's the AngularJS code, which logs Export-Response:[object Object]
:
exportProjects() {
let filteredProjectIds = ;
for (let i in this.filteredProjects) {
for (let x = 0, l = this.filteredProjects[i].length; x < l; x++) {
if (!this.isOldProjectsBundle(this.filteredProjects[i][x])) {
filteredProjectIds.push(this.filteredProjects[i][x].id);
}
}
}
this.$http.get('/profiles/projectWordExport?filteredProjects=' + filteredProjectIds.join(",")).then(response => {
console.log("Export-Response:" + response);
return response;
});
}
This is the Java code being called (it's really being called, already debugged it, no errors occuring):
@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET)
public void getProjectsWord(HttpServletRequest request, HttpServletResponse response, @RequestParam String filteredProjects) throws Exception {
//Load project objects from input string or load all projects if input empty
List<Project> projects = new java.util.ArrayList<>();
if (filteredProjects.isEmpty()) {
projects = projectRepository.findAll();
} else {
String pIds = filteredProjects.split(",");
for (String pId : pIds) {
projects.add(projectRepository.findById(Long.parseLong(pId)));
}
}
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-disposition", "attachment;filename=Projektexport.docx");
try {
SaveToZipFile saver = new SaveToZipFile(printer.printProjects(this.prepareProjectExport(projects)));
saver.save(response.getOutputStream());
response.flushBuffer();
} catch (NullPointerException e) {
response.setStatus(500);
response.sendError(500, "Fehler beim exportieren des Tests aufgetreten");
}
}
angularjs spring download
angularjs spring download
asked Nov 20 '18 at 10:27


Cold_ClassCold_Class
793826
793826
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Put this in @RequestMapping annotation
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
I changed it to@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
but nothing has changed :(
– Cold_Class
Nov 20 '18 at 12:17
The method should return byte (byte array) instead of void. Give it a try and tell what happens.
– K.Kostadinov
Nov 20 '18 at 13:00
this might help as well: response.body(new InputStreamResource(inputStream));
– K.Kostadinov
Nov 20 '18 at 13:07
But I would have to return something then if I change the return type...how? And how should I fill "inputStream" and which type is it?
– Cold_Class
Nov 20 '18 at 15:01
Initialize ByteArrayOutputStream, wrap the file in FileInputStream, fill the baos and return baos.toByteArray()
– K.Kostadinov
Nov 21 '18 at 12:21
|
show 3 more comments
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53390944%2fspring-get-request-file-not-being-downloaded%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
Put this in @RequestMapping annotation
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
I changed it to@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
but nothing has changed :(
– Cold_Class
Nov 20 '18 at 12:17
The method should return byte (byte array) instead of void. Give it a try and tell what happens.
– K.Kostadinov
Nov 20 '18 at 13:00
this might help as well: response.body(new InputStreamResource(inputStream));
– K.Kostadinov
Nov 20 '18 at 13:07
But I would have to return something then if I change the return type...how? And how should I fill "inputStream" and which type is it?
– Cold_Class
Nov 20 '18 at 15:01
Initialize ByteArrayOutputStream, wrap the file in FileInputStream, fill the baos and return baos.toByteArray()
– K.Kostadinov
Nov 21 '18 at 12:21
|
show 3 more comments
Put this in @RequestMapping annotation
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
I changed it to@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
but nothing has changed :(
– Cold_Class
Nov 20 '18 at 12:17
The method should return byte (byte array) instead of void. Give it a try and tell what happens.
– K.Kostadinov
Nov 20 '18 at 13:00
this might help as well: response.body(new InputStreamResource(inputStream));
– K.Kostadinov
Nov 20 '18 at 13:07
But I would have to return something then if I change the return type...how? And how should I fill "inputStream" and which type is it?
– Cold_Class
Nov 20 '18 at 15:01
Initialize ByteArrayOutputStream, wrap the file in FileInputStream, fill the baos and return baos.toByteArray()
– K.Kostadinov
Nov 21 '18 at 12:21
|
show 3 more comments
Put this in @RequestMapping annotation
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
Put this in @RequestMapping annotation
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
answered Nov 20 '18 at 11:47
K.KostadinovK.Kostadinov
863
863
I changed it to@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
but nothing has changed :(
– Cold_Class
Nov 20 '18 at 12:17
The method should return byte (byte array) instead of void. Give it a try and tell what happens.
– K.Kostadinov
Nov 20 '18 at 13:00
this might help as well: response.body(new InputStreamResource(inputStream));
– K.Kostadinov
Nov 20 '18 at 13:07
But I would have to return something then if I change the return type...how? And how should I fill "inputStream" and which type is it?
– Cold_Class
Nov 20 '18 at 15:01
Initialize ByteArrayOutputStream, wrap the file in FileInputStream, fill the baos and return baos.toByteArray()
– K.Kostadinov
Nov 21 '18 at 12:21
|
show 3 more comments
I changed it to@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
but nothing has changed :(
– Cold_Class
Nov 20 '18 at 12:17
The method should return byte (byte array) instead of void. Give it a try and tell what happens.
– K.Kostadinov
Nov 20 '18 at 13:00
this might help as well: response.body(new InputStreamResource(inputStream));
– K.Kostadinov
Nov 20 '18 at 13:07
But I would have to return something then if I change the return type...how? And how should I fill "inputStream" and which type is it?
– Cold_Class
Nov 20 '18 at 15:01
Initialize ByteArrayOutputStream, wrap the file in FileInputStream, fill the baos and return baos.toByteArray()
– K.Kostadinov
Nov 21 '18 at 12:21
I changed it to
@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
but nothing has changed :(– Cold_Class
Nov 20 '18 at 12:17
I changed it to
@RequestMapping(value = "/projectWordExport", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
but nothing has changed :(– Cold_Class
Nov 20 '18 at 12:17
The method should return byte (byte array) instead of void. Give it a try and tell what happens.
– K.Kostadinov
Nov 20 '18 at 13:00
The method should return byte (byte array) instead of void. Give it a try and tell what happens.
– K.Kostadinov
Nov 20 '18 at 13:00
this might help as well: response.body(new InputStreamResource(inputStream));
– K.Kostadinov
Nov 20 '18 at 13:07
this might help as well: response.body(new InputStreamResource(inputStream));
– K.Kostadinov
Nov 20 '18 at 13:07
But I would have to return something then if I change the return type...how? And how should I fill "inputStream" and which type is it?
– Cold_Class
Nov 20 '18 at 15:01
But I would have to return something then if I change the return type...how? And how should I fill "inputStream" and which type is it?
– Cold_Class
Nov 20 '18 at 15:01
Initialize ByteArrayOutputStream, wrap the file in FileInputStream, fill the baos and return baos.toByteArray()
– K.Kostadinov
Nov 21 '18 at 12:21
Initialize ByteArrayOutputStream, wrap the file in FileInputStream, fill the baos and return baos.toByteArray()
– K.Kostadinov
Nov 21 '18 at 12:21
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53390944%2fspring-get-request-file-not-being-downloaded%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