pdf download that is outside the java jsf application

Asked

Viewed 448 times

0

need to download some pdf that are outside of my application, are in the folder c: tmp\

how could I use primefaces to make these files available for download?

I thought of saving in folder C: jboss-as-7.1.1.Final standalone Deployments app.War WEB-INF

but each time I deploy this file is deleted

1 answer

1

Try this:

Declare an attribute of type DefaultStreamedContent in your bean: Example:

private DefaultStreamedContent download;

Do the Get and Sets:

public DefaultStreamedContent getDownload() {
        return download;
    }

public void setDownload(DefaultStreamedContent download) {
        this.download = download;
}

Next :

    //Método que faz o download do anexo
    public void prepDownload() throws Exception {
        File file = new File("c:/tmp/arquivo.pdf");

        InputStream input = new FileInputStream(file);
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    }

Then on your button;

<p:commandButton icon="ui-icon-arrowthickstop-1-s"
    title="Download Anexo"
    actionListener="#{bean.prepDownload()}"
    ajax="false">
    <p:fileDownload value="#{bean.download}" />
</p:commandButton>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.