I want to download a file from the front end. The file is generated in the TestFlow.generateReport
method.
My code runs until the end but it doesn't download anything. It just prints the data to the console.log
.
What am I missing here?
My backend controller:
@RequestMapping(value = "/flow/generate-report" , method = RequestMethod.GET)
public @ResponseBody void generateFlowReport(@RequestParam("flowName") String flowName, HttpServletResponse response) {
InputStream resource = TestFlow.generateReport(flowName);
response.setContentType("application/force-download");
response.setHeader("Content-Disposition","attachment; filename=report-" + flowName + ".xlsx");
try {
IOUtils.copy(resource,response.getOutputStream());
response.flushBuffer();
resource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
My frontend code:
$('.generateReport').click(function () {
var flowName = $(this).attr('name');
$.ajax({
url: '/flow/generate-report',
type: 'get',
data: {flowName: flowName},
success: function (data) {
console.log(data);
}
})
});
I get HTTP status 200
HTTP/1.1 200
Content-Disposition: attachment; filename=report-tellerFlow.xlsx
Content-Type: application/xlsx
Transfer-Encoding: chunked