Search code examples
spring-mvcdownloadresponse

SpringMVC download file,the response result has data but explorer can't download file


As the title says. I set the values in java like this:

response.setHeader("Content-Disposition", "attachment; filename=" + targetName);
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");

I debugged in chrome explorer, and the response body is:

enter image description here


Solution

  • Question solved by changing the submit method in JS file. At first I send request by this: Ext.Ajax.request({ utl:'', params:{}, method:'POST' }); Butajax can't download a file but can receive json/html/js/string format body. So in extjs, if you want to download file, DO NOT USE ajax request. And maybe you'll need use native javascript to do it. For example: var form = document.createElement("form); form.method='POST'; form.action='/'; //deal with data

    document.body.appendChild(form);
    form.submit();