Jorge Rocha
Jorge Rocha

Reputation: 724

h:commandButton with oncomplete function and download a file

I have a little problem because i need to download a file (and for that i only can with h:commandButton) but i needed the oncomplete function (that only works with a4j:commandButton) to appear a Running Status. The sample of the code is this:

 <h:commandButton
        id="downloadReportButton"
        action="#{reportingBean.createAndDonwloadFile()}"               
 />

I already try to do it like in this response by BalusC: https://stackoverflow.com/a/31267418/1777424 but the problem is because of Ajax requests are not perform.

<h:commandButton
        id="downloadReportButton"
        action="#{reportingBean.createAndDonwloadFile()}"/>
        <f:ajax onevent="oneventFunction" />
 </h:commandLink>

function oneventFunction(data) {
    if (data.status === "success") {
        oncompleteFunction();
    }
}

Upvotes: 1

Views: 3983

Answers (1)

Jorge Rocha
Jorge Rocha

Reputation: 724

I found the way to resolve this. First is needed to have a a4j:commandButton just to have the click button with ajax requests. In this part the file will be created.

<a4j:commandButton
    id="createFileButton"
    onclick="showLoading();"
    action="#{reportingBean.createFile()}"
    oncomplete="hideLoading(); #rich:element('downloadFileButton')}.click();"
/>

This a4j:commandButton will in oncomplete click on downloadFileButton button that will download the file. Then is necessary to have a h:commandButton to download the file that have been created.

<h:commandButton
    id="downloadFileButton"
    action="#{reportingBean.downloadFile()}"
    style="display:none"
/>

This button will be hidden (style="display:none") because is only to trigger the download of the file.

Upvotes: 2

Related Questions