I have a commandButton which is calling a java method to generate an excel file, my question is : is it possible to call a javascript function once the file has been generated ?
Here is the code I use in the jsp :
Code: Select all
<p:commandButton id="extractionDataRegional"
actionListener="#{exportController.doExport}" ajax="false"
oncomplete="myJavascriptFunction()"
value="Do export" update="messages" />
Code: Select all
public static void doExport() throws IOException, InvalidFormatException {
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
.getResponse();
response.setContentType("application/vnd.ms-excel");
String headerResponse = "attachment;filename=";
headerResponse = headerResponse.concat(fileName);
response.addHeader("Content-disposition", headerResponse);
ServletOutputStream out = response.getOutputStream();
Workbook wb = WorkbookFactory.create(inputStream);
...........
wb.write(out);
out.flush();
out.close();
context.responseComplete();
The funny thing is that when I put ajax="true" the javascript function is called, but the file is not downloaded !!!
I have searched in the PrimeFaces user's guide, there I found that CommandButton component submits it's enclosed form, so I tried specifying the javascript function in the onsubmit flag of the form, but this function is not called after but before the file is generated. So this option does not solve my problem.
Has anyone any idea ???
Thx