Thursday, 12 September 2019

Subscribe didn't receive the response to save .docs file

In my project, I'm using angular6 with Springboot backend. So what I'm trying to do is popup the save dialog using this controller java class,

@RequestMapping(value = "/getGeneratedLetter/{userId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void genarateLetter(@PathVariable("userId") Integer userId, HttpServletResponse response) {
        System.out.println(userId);
        letterTypeService.genarateLetter(userId);
        try {
            // get Letter.docx from file path
            InputStream is = new FileInputStream("Letter.docx");
            response.setHeader("Content-Disposition", "attachment; filename = Letter.docx");
            IOUtils.copy(is, response.getOutputStream());
            response.flushBuffer();
        } catch (Exception ex) {
            throw new RuntimeException("IOError in " + ex);
        }
    }

Note: in my letterTypeService java class the letter will be generated correctly.

And finally, I'm catching the response in my angular front-end in my component,

getGenaratedLetterTemplate(letter) {
  this.data.getGenaratedLetterTemplate(letter.letterId).subscribe(response => {
    this.getBasicDataFromBackend();
    saveAs(response,  letter.fullName + '_' + letter.letterTypeName + '.docx');
    console.log(response);
  }, error => {
    console.log(error);
  });
}

In above function responce will not catch since the result is

error massage: "OK"

It's confusing because it displayed just "OK" there are not any specific error details. Additionally in my service in frontend successfully send get request and in-network tab in chrome dev tools displays "status code:200" enter image description here

Further investigation When I click the URL in my get request the letter download popup will be displayed successfully and when I click the save button the letter will download successfully.

So what could be the issue here?



from Subscribe didn't receive the response to save .docs file

No comments:

Post a Comment