web-dev-qa-db-fra.com

Le téléchargement du fichier Primefaces ne fonctionne pas?

Essayer de faire fonctionner un téléchargement de fichier simple et tout ce que je reçois est une barre d'état AJAX et c'est tout. Mes sorties de bean backing affichent le nom correct sur la préparation et le téléchargement.

Suis-je en train de faire ça mal? les deux sorties me paraissent correctes.

JSF 2.0 Primefaces 3.4

        <h:form>
            <p:commandButton id="downloadLink" value="Download" actionListener="#{filemanagement.prepDownload}"> 
                <p:fileDownload value="#{filemanagement.download}" />
            </p:commandButton>
        </h:form>

Haricot de support:

private DefaultStreamedContent download;

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

public DefaultStreamedContent getDownload() throws Exception {
    System.out.println("GET = " + download.getName());
    return download;
}

public void prepDownload() throws Exception {
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
}
16
user2124871

Voir Primefaces Documentation 6.1

Si vous souhaitez utiliser le bouton de commande PrimeFaces et commandLink, désactivez l'option ajax car fileDownload nécessite une actualisation de la page complète pour présenter le fichier.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false" actionListener="#{filemanagement.prepDownload}">
    <p:fileDownload value="#{filemanagement.download}" />
  </p:commandButton>
</h:form>
28
Manuel

Dans le bouton de commande, définissez ajax = false et n'utilisez pas d'action ou d'écouteur d'action pour commandlink.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false">
    <p:fileDownload value="#{filemanagement.downloadValue}" />
  </p:commandButton>
</h:form>

Haricot:

public StreamedContent getDownloadValue() throws Exception {
    StreamedContent download=new DefaultStreamedContent();
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    download = new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
    return download;
}
14
Manbumihu Manavan

La chose la plus importante que j'ai faite est de tourner ajax = "false".

Ici, j'ai avec commandlink, comme vous pouvez le voir ci-dessous.

En html:

<p:commandLink id="someId"
                value="Download"
                style="padding-left: 2em; vertical-align: middle;"
                ajax="false"
                onstart="PF('pageBlocker').show()"
                oncomplete="PF('pageBlocker').hide()">
  <p:fileDownload value="#{bean.downloadFileTemplate()}" />
</p:commandLink>

En Java:

public StreamedContent downloadFileTemplate() {
    try {
        FileInputStream inputStream = new FileInputStream(new File(getClass().getClassLoader().getResource("path_to_resource/myfile.xlsx").getFile()));

        StreamedContent fileTemplate = new DefaultStreamedContent(
                inputStream
                , "application/vnd.ms-Excel"
                , "my_file.xlsx");

        return fileTemplate;
    } catch (Exception e) {
        getLog().error("Error on download...", e);
        return null;
    }
}
0
Lucas Campos