0

I might be missing something very obvious, but no matter what I try to get the download button working, it will only work if I use POST on the full form, but not with GET, which I need (I don't wan1t to post the form, becaue I'm not giving back a String answer JSP is expecting, but rather an attachment to download).

So I got a JSP page with a form that has a button in it that is for downloading a generated xls file. If I post the form I get the xls back to download. here is the code:

@RequestMapping(params = "exportButton", method = RequestMethod.POST)
public void exportXls(Model model, @ModelAttribute("form") XXXForm form, HttpServletResponse response)
        throws IOException {

            // generate xls...

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=xxx.xls");
        workbook.write(response.getOutputStream()); //an api to generate xls
        workbook.close();
        response.flushBuffer();

    }

}

So this works great when posting:

<form:form id="xxxForm" method="post" modelAttribute="xxxForm" cssClass="cleanform" action="xxx">    
<button type="submit" id="exportButton" name="exportButton" class="printButton">Export</button>

But if i try using get:

<button type="button" id="exportButton" class="printButton" onclick="getReportInXls()">Export</button>

Javascript:

function getReportInXls() {
    $('#progressIndicatorImage').show();
    $('#exportButton').hide();
    $.ajax({
        url : 'getListXml',
        success : function(data) {
            $('#progressIndicatorImage').hide();
            $('#exportButton').show();
        }
    });
}

And Java is same with the mapping changed. The function gets called, the xls is generated, and the response arrives, I get a 200-OK response, with the attachment in the body (binary), and in the header there is the attachment in the Content-Disposition:

Response header:

Cache-Control   
no-cache, no-store
Content-Disposition 
attachment; filename=hotel.xls
Content-Type    
application/vnd.ms-excel
Date    
Wed, 29 Jul 2015 09:27:59 GMT
Expires 
Thu, 01 Jan 1970 00:00:00 GMT
Pragma  
no-cache
Server  
Apache-Coyote/1.1
Transfer-Encoding   
chunked

Yet, I get no offer to download/view the file. Nothing happens on browser side. I tried quiet a few things on both server and client side. I've written the @ResponseBody annotation, I tried adding @RequestMapping(value = "/getXls", method = RequestMethod.GET, produces = "application/vnd.ms-excel"), tried the force download, nothing seem to work.

Note: The I've changed the names, so it might be getXls and something like getList the other time, but that's not the problem. My server side function gets called, the xls is generated fine, and I get a 200-OK back with seemingly the correct header/body! I just don't get to download/view my file... Tested in two versions of firefox and in chrome.

Revolit
  • 155
  • 1
  • 3
  • 14

1 Answers1

1

Your GET request is working correctly.

This is what happens: When you do the POST variant, the browser itself handles the response with the attachment and asks you if you want to download/view the attachment.

When you run the GET variant, your javascript callback to $.ajax receives the response (and the browser does not intervene). And since it does nothing with the data, nothing happens (except that you toggle visibility of the indicator and button).

To handle your original problem (how to download without a form post) please read this question and it answers.

wero
  • 32,544
  • 3
  • 59
  • 84