Help with monitorDownload api ...

UI Components for JSF
Post Reply
3man
Posts: 99
Joined: 16 Jun 2011, 16:13

06 Dec 2011, 14:19

Hello,

can someone tell me is it possible to get p:fileDownload work on ajax request?

I need this because I need to control ajax status :
onstart .. show ajaxloadingbar.gif
onerror .. show error in growl
onsuccess .. hide ajaxloader.gif, and write success message in growl

Right now, code below work well, but when user click on button, there is no some indicator that showing him "Generating report is in progress .. please wait", or "Error while generating report ..."

If I remove ajax tag, then p:filedownload doesn't work .. but report is generated on server side.
Can some one help me with this?

This is snippet of code:
report.xhtml

Code: Select all

        <p:commandButton value="Report" title="Report" styleClass="reportButton" actionListener="#{servis.generateReport}" ajax="false" >
            <p:fileDownload contentDisposition="inline" value="#{servis.report}"  />
        </p:commandButton>


Servis.java

Code: Select all

 
@ManagedBean(name = "servis")
@RequestScoped
public class Servis
{

    private StreamedContent report;

    public StreamedContent getReport() throws FileNotFoundException {

        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        File result = new File(extContext.getRealPath("//resources//reports//pdf//test1.pdf"));
        InputStream reportStream = new FileInputStream(result.getAbsolutePath());
        
        return new DefaultStreamedContent(reportStream, "application/pdf; charset=UTF-8", "test1.pdf");        
    }

    public void setReport(StreamedContent report) {
        
        this.report = report;
    }

    public void generateReport(ActionEvent ae) 
    {
        HashMap parameterMap = new java.util.HashMap();
        parameterMap.put("id_tipa", this.id_tipa);
        
        db.report2Pdf("/home/user_name/Projects/java/jsf/testJSF_5/web/resources/reports/jasper/test1.jasper", "/home/user_name/Projects/java/jsf/testJSF_5/build/web/resources/reports/pdf/test1.pdf", parameterMap);

    }

Last edited by 3man on 20 Feb 2012, 20:10, edited 1 time in total.
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

06 Dec 2011, 15:05

It is not with current implementation.

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

06 Dec 2011, 15:12

Thanks for replay .. can you suggest some other solution?
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

07 Dec 2011, 10:32

Ok, I find workaround

Create two button:
First button for creating report(ajax=true). While creating report show images/ajaxloadingbar.gif.
On success click on second button, which is hide.
Second button(ajax=false), download generated report.

Code: Select all

<p:commandButton value="Report" title="Report" styleClass="reportButton" actionListener="#{servis.generisiIzvestaj}" onsuccess="document.getElementById('frmContent:fakeReportButton').click(); " />
<p:commandButton id="fakeReportButton" ajax="false" styleClass="reportButton" style="display: none;">
      <p:fileDownload contentDisposition="inline" value="#{servis.report}"  />
</p:commandButton>
Not so happy with this solution :/ .. but it do the job ..
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

11 Jan 2012, 14:48

3.1 and 3.0.1 has monitorDownload api to display status of file download to the user;

Code: Select all

<script type="text/javascript">
function showStatus() {
    statusDialog.show();
}

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

<h:form>

    <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false">
        <p:graphicImage value="/design/ajaxloadingbar.gif" />
    </p:dialog>
			
    <p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)">
        <p:fileDownload value="#{fileDownloadController.file}"/>
    </p:commandButton>
			
</h:form>
So when download starts, modal dialog is shown and kept visible until download completes.

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

20 Feb 2012, 20:09

I try with version 3.1.1 and it didn't work ..
modal dialog is shown and keep visible when download completes.

Any hint?

this is code I use:
index.xhtml:

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:head>
        <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
        <title>TEST</title> 

        <script type="text/javascript">
            function showStatus() {
                statusDialog.show();
            }

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


    </h:head>
    
    <h:body>

        <h:form>

            <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false">
                <p:graphicImage value="./resources/images/ajaxloadingbar.gif" />
            </p:dialog>

            <p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)">
                <p:fileDownload contentDisposition="inline" value="#{test.report}"/>
            </p:commandButton>

        </h:form>



    </h:body>

</html>
test.java

Code: Select all

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import fs.org.beans.programs.Narucioci;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author sloba
 */
@ManagedBean
@RequestScoped
public class test  {
    private StreamedContent report;
    
     public test() {}

    public void setReport(StreamedContent report) {
        this.report = report;
    }

    public StreamedContent getReport() {
        try {
            
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Narucioci.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            String pdf_path = "/home/sloba/Desktop/narucioci.pdf";
            ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
            extContext.setResponseCharacterEncoding("UTF-8");
            extContext.setResponseContentType("inline/pdf");
            extContext.setResponseHeader("Content-Disposition", "inline");

            File file = new File(pdf_path);
            InputStream reportStream = new FileInputStream(file.getAbsolutePath());
            
            if(!file.exists()){
                System.out.println("No file on path: "+pdf_path);
                return null;
            }
            

            return new DefaultStreamedContent(reportStream, "inline/pdf; charset=UTF-8", "narucioci.pdf"); 
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Narucioci.class.getName()).log(Level.SEVERE, "", ex);
        }
        
        return null;

    }
    
}
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 17 guests