Show modal while downloading file

0

I have a button whose function is to download a file, however this process is sometimes very long and the ideal is to show a message of "loading" while the file is generated, but when using ajax = "false I can not use the functions onStar PrimeFaces.monitorDownload but it does not work for me:

xhtml:

 <p:commandLink id="btnExportar" class="boton"
                                   value="Exportar a Excel"
                                   action="#{consultaCtrl.exportar}"
                                   onclick="PrimeFaces.monitorDownload(showStatus(), hideStatus())"
                                   ajax="false"/>
                    <p:tooltip id="toolTipExportar" for="btnExportar" value="Exporta filas visibles a Excel"   position="left"/>

  <p:dialog widgetVar="cargando" modal="true" draggable="false" closable="false" resizable="false" showHeader="false" styleClass="small centered-all">
        <i class="fa fa-spinner fa-spin" style="font-size:24px"></i>
    </p:dialog> 

<script>
        function showStatus() {
            PF('statusDialog').show();
        }

        function hideStatus() {
            PF('statusDialog').hide();
        }
    </script>

Controller:

public void exportar() {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    String name = periodo.getLabel() + ".xls";
    ec.responseReset();
    ec.setResponseHeader("Content-Type", "application/vnd.ms-excel");
    ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + name + "\"");

    try (OutputStream output = ec.getResponseOutputStream()) {
        exportarExcelService.exportar((List<Guia>) guias.getAllPagesGuias(), periodo.getLinea(), output);
        try (CountingOutputStream co = new CountingOutputStream(output)) {
            System.out.println("termine");
            ec.setResponseContentLength((int) co.getByteCount());
            co.close();
        } catch (IOException ex) {
            System.out.println("Error al obtener el tamaño del stream " + ex.getMessage());
        }
    } catch (IOException e) {
        System.out.println("Error al intentar crear el excel " + e.getMessage());
    }

    fc.responseComplete();

}
    
asked by gibran alexis moreno zuñiga 12.05.2017 в 23:24
source

1 answer

0

I had the same problem but I solved it in the following way, it also helps you to use it in a dialog.

<h:commandButton id="btnExportar" 
                 actionListener="#{consultaCtrl.exportar}" 
                 value="Exportar a Excel" 
                 class="estilo_botones"
                 onclick="PF('statusDialog').show();"
                 onsuccess="PF('statusDialog').hide();">

      <f:ajax execute="@all" render="@form"/>

</h:commandButton>
<p:tooltip id="toolTipExportar" for="btnExportar" value="Exporta filas visibles a Excel"   position="left"/>


<p:dialog modal="true" widgetVar="statusDialog" header="Procesando" draggable="false" closable="false" resizable="false">
    <p:graphicImage value="/resources/images/ajaxloadingbar.gif"/>
</p:dialog>
    
answered by 13.05.2017 в 00:08