I'm making an ajax call from a jsp to a servlet.
var xhr = new XMLHttpRequest();
function download(){
xhr.open("GET", "/FileDownloader?<params>");
xhr.send();
}
In the servlet, I'm creating an Excel file (POI HSSFWorkbook) and can successfully write the xls to a file but cannot send it back to the browser as a download:
private void ResultsXLS(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Expires", "0");
resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
resp.setHeader("Pragma", "public");
resp.setHeader("Content-Disposition", "attachment; filename=export.xls");
try{
//Datbase query - populates workbook
HSSFWorkbook wb = new HSSFWorkbook();
//THIS WORKS - Excel file is flawless
//try (FileOutputStream outputStream = new FileOutputStream("C:/TEMP/ic50s.xls")){
//wb.write(outputStream)
//outputStream.close();
//THIS DOESN'T WORK
ServletOutputStream out = resp.getOutputStream();
wb.write(out);
out.flush();
out.close();
}
catch (Exception e) {
LOG.log(Level.SEVERE, e.toString(), e );
} finally {
//cleanup
}
}
I don't see any errors anywhere. How can I further debug?
Thank you!