user3631948
user3631948

Reputation: 85

Download ZIP from Java After Ajax-Call

I'm having a problem, I create a ZIP file with PDF files in it in JAVA

@Override
@ResponseBody
@RequestMapping(value = "pdfDownload", method = RequestMethod.POST, produces = "application/zip")
public byte[] generatePDFs(
        @RequestParam(value = "date", required = true) String date,
        Principal principal,
        HttpServletResponse response) {

    String filename;
    PrintRequest pr = new PrintRequest ();
    List<String> pis = new ArrayList<> ();
    pis.add ("3453453535");
    pr.setDate ("032015");
    pr.setPids (pis);

    filename = pr.getDate () + ".zip";
    response.setContentType ("application/zip");
    response.setHeader("Content-Disposition", "attachment; filename="+ filename);

    return createZip (pr, principal);
}

Creating the ZIP file works fine, but if I call this with jQuery AJAX POST I get back the bitstream as result but cannot download the file.

Btw. the file is created on the fly and do not lie on the server.

Hope someone can help me.

Best regards!

Upvotes: 0

Views: 1731

Answers (1)

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17435

If you make a request to this method in an ajax call, the resulting bytestream will be in the result object in javascript. That's not useful at all.

You shouldn't be doing this in an ajax call at all. Either generate a normal html link pointing to the request url, or build the url in javascript and put it in window.location.

If you need to send a POST instead, make an html form and post that. You can populate the data with javascript and then invoke submit(). Check here for more details: JavaScript post request like a form submit

Upvotes: 2

Related Questions