0

I am unable to download the zip file. After clicking ok button in confirmDialog I am unable to download a zip file. If I am not using confirmDialog I can able to download the file. Could any one help me on this,

Here I added the code for the reference.

<h:form>
    <p:panelGrid rendered="true">
        <p:commandButton style="HEIGHT: 24px; WIDTH: 300px" id="AjaxFalse"
            value="AjaxFalse"
            action="#{testDownload.getZipFile}"
            title="AjaxFalse" rendered="true">
            <p:confirm message="Are you sure?"
                header="Confirmation"
                icon="ui-icon-alert" />
        </p:commandButton>

    </p:panelGrid>

    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade" width="500">
                <h:panelGroup layout="block" style="text-align: center">
                    <p:commandButton value="Ok"  type="submit" styleClass="ui-confirmdialog-yes" style="width:70px;"/>
                    <p:commandButton value="Cancel" type="button" styleClass="ui-confirmdialog-no" style="width:70px;"/>
                </h:panelGroup>
    </p:confirmDialog>
</h:form>

My backingbean is

public class TestDownload
{
//some code here
//Downloading the zip file in jsf2.x
public String getZipFile()
    {       
    try{
    //enter code here
            FacesContext fc = FacesContext.getCurrentInstance();
            ExternalContext ec = fc.getExternalContext();
            ec.setResponseHeader("Content-Disposition", "attachment;filename=\"" + Test + ".zip\"");
            ec.setResponseCharacterEncoding("UTF-8");
            ec.setResponseContentType("application/zip");
            //This method will generate the required file.
            new BulkloadImporter().generateIntegrationXSDs(getId(), ec.getResponseOutputStream());

            fc.responseComplete();
            return "Save";
        }
        catch(Exception e)
        {
            //Handling exception
        }

    }
    //some code here    
}
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • 1
    If I remove tag, and adding ajax="false"for button, then its working as excepted.But I use then unable to download. – Kaviyarasan Mannan Nov 24 '15 at 10:59
  • I would expect a `p:confirm` and `ajax=false` combination to also work. Downloading in 'ajax=true' does not work... – Kukeltje Nov 24 '15 at 11:09
  • Combination of the p:confirm and ajax=false also not working, for this combination control not at all going to the bean method. – Kaviyarasan Mannan Nov 24 '15 at 11:12
  • So that is your problem... https://github.com/primefaces/primefaces/issues/99 – Kukeltje Nov 24 '15 at 11:16
  • Off-Topic: read this: http://stackoverflow.com/questions/9391838/how-to-provide-a-file-download-from-a-jsf-backing-bean?lq=1 (especially the 'OmniFaces' Faces.sendFile(...) thing... – Kukeltje Nov 24 '15 at 22:43

1 Answers1

6

This issue is not 'download' related. Up to now (PrimeFaces 5.3/5.3.2) using a <p:confirm...> global dialog in combination with ajax="false" does not work. The server side action in general will not be called.

There are two possible solutions:

  • Don't use global mode and do the download on the button in the confirmDialog
  • Use the workaround mentioned in the issue (not tested this myself)

An example of non-global usage can be found in the PrimeFaces Documentation (adapted to this case):

Getting started with ConfirmDialog ConfirmDialog has two modes; global and non-global. Non-Global mode is almost same as the dialog component used with a simple client side api, show() and hide().

<h:form>
  <p:commandButton type="button" onclick="PF('cd').show()" />
  <p:confirmDialog message="Are you sure about downloading this file?"
header="Initiating download" severity="alert" 
widgetVar="cd">
     <p:commandButton value="Yes Sure" action="#{testDownload.getZipFile}" ajax="false"
update="messages" oncomplete="PF('cd').hide()"/>
     <p:commandButton value="Not Yet" onclick="PF('cd').hide();" type="button" />
  </p:confirmDialog>
</h:form>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Wow, 3 upvotes on an answer that could be found by just searching google, looking at the PrimeFaces issuelist and documentation. – Kukeltje Nov 25 '15 at 08:40