0

I am working on a jsp application and I have a download zip file feature

I have done

 @RequestMapping(value = {​​"/retrieve"}​​, method = RequestMethod.POST)
    public void logDownload(@RequestBody ResponseDto responseDto, Model model, HttpServletResponse response) throws ServletException, IOException {​​
    
    Byte[] arraObj=downloadService.getdownloadFile();
response.setContentType("application/octate-stream");
response.setHeader("Content-Disposition","attachment;filename=user.zip", 
    response.getOutputStream().write(arraObj);
    }

I am making a ajax call for this controller

JSP Side code

$.ajax({
    type: 'POST',
    url: "${pageContext.request.contextPath}/retrieve",
    headers: { "X-CSRF-TOKEN": token },
    contentType: "application/json",
    dataType: 'json',
    data: JSON.stringify(retrieveDto),
    success: function (res) {
        window.location = "${pageContext.request.contextPath}/result";

    },
    error: function (e) {
        // window.location="${pageContext.request.contextPath}/result";
        console.log("There was an error with your request..." + JSON.stringify(e));
    }

});

I am unable to download the file Can someone tell me how to fix this issue

Thejas
  • 379
  • 5
  • 24
  • What debugging have you done so far? Can you confirm that your logDownload method is called each time you click in your jsp application? Have you debugged your jsp application to see if it is properly receiving the response from the controller? How does your jsp application create a folder when you get the response? If you could post some of your jsp application (the onClick and processResponse areas), that could help. – Mark Brown Mar 30 '22 at 13:39
  • @MarkBrown Yes for each click the controller is getting invoked, Let m update the jsp part – Thejas Mar 30 '22 at 13:40
  • Thejas I don't see where you are saving the file in your jsp code. Have you looked at: https://stackoverflow.com/questions/13405129/javascript-create-and-save-file – Mark Brown Mar 30 '22 at 19:15
  • @MarkBrown for the second click the files gets downloaded to a folder – Thejas Mar 31 '22 at 06:30

0 Answers0