Spring get request file not being downloaded












1















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");
}
}









share|improve this question



























    1















    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");
    }
    }









    share|improve this question

























      1












      1








      1








      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");
      }
      }









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 10:27









      Cold_ClassCold_Class

      793826




      793826
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Put this in @RequestMapping annotation



          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE





          share|improve this answer
























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          Put this in @RequestMapping annotation



          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE





          share|improve this answer
























          • 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
















          0














          Put this in @RequestMapping annotation



          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE





          share|improve this answer
























          • 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














          0












          0








          0







          Put this in @RequestMapping annotation



          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE





          share|improve this answer













          Put this in @RequestMapping annotation



          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          How to fix TextFormField cause rebuild widget in Flutter