How to use p: fileDownload to download a specific file

0

I have a datatable and I want to implement a download button to download the pdf file from that particular line. I have already been able to implement p: fileUpload and now I want to download this file that was uploaded.

My upload method

public void upload(FileUploadEvent evento) {
    try {
        UploadedFile arquivoUpload = evento.getFile();
        Path arquivoTemp = Files.createTempFile(null, null);
        Files.copy(arquivoUpload.getInputstream(), arquivoTemp, StandardCopyOption.REPLACE_EXISTING);
        monografia.setCaminho(arquivoTemp.toString());

        Messages.addGlobalInfo("Upload realizado com sucesso");
    } catch (IOException erro) {
        Messages.addGlobalInfo("Ocorreu um erro ao tentar realizar o upload de arquivo");
        erro.printStackTrace();
    }
}

Method salvar

public void salvar(){
    try {

        Path origem = Paths.get(monografia.getCaminho());
        Path destino = Paths.get("C:/Uploads/Monografia/" + monografia.getTitulo() + ".pdf");

        Files.copy(origem, destino, StandardCopyOption.REPLACE_EXISTING);

        cadastroMonografiaService.salvar(monografia);

        atualizarRegistros();

        messages.info("Monografia salva com sucesso!");

        RequestContext.getCurrentInstance().update(Arrays.asList("frm:monografiasDataTable", "frm:messages"));

    } catch (Exception erro) {
        messages.alerta("Erro ao salvar a Monografia!");
        erro.printStackTrace();
    }
}
    
asked by anonymous 03.05.2018 / 00:58

2 answers

0

I solved this way

My method getFile

public StreamedContent getFile() throws FileNotFoundException {
    try{
        InputStream stream = new FileInputStream("C:/Uploads/Monografia/" + monografia.getTitulo() +".pdf");
        file = new DefaultStreamedContent(stream, "application/pdf", monografia.getTitulo()+".pdf");

    } catch (IOException erro) {
        messages.alerta("Não foi possível fazer o download da Monografia ");
        erro.printStackTrace();
    }
    return file;
}

Component fileDownload

            <p:column headerText="Download">
                <p:commandButton immediate="true" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-arrowthick-1-s">
                    <p:fileDownload value="#{gestaoMonografiasBean.file}" />
                </p:commandButton>
            </p:column>
    
27.05.2018 / 23:34
1

In your table you will have a list of objects, these objects must have the pdf or provide a way to reach it. In your view you create an attribute and a method as below:

private StreamedContent file;

//demais atributos metodos omitidos
public void fileDownload(MeuItem meuItem){
    //buscando o pdf, usar o 'meuItem' para definir a busca desse pdf
    InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/demo/images/optimus.jpg");

    //atribuindo o pdf ao file
    file = new DefaultStreamedContent(stream, "pdf", "meupdf.pdf");
}

public StreamedContent getFile() {
    return file;
}

In your table you do this:

<p:dataTable id="minhaTable" var="meuItem" value="#{minhaView.meusItens}">
    <f:facet name="header">
        Basic
    </f:facet>
    <p:column headerText="Id">
        <h:outputText value="#{meuItem.id}" />
    </p:column>
    <p:column headerText="Year">
        <h:outputText value="#{meuItem.descricao}" />
    </p:column>
    <p:column headerText="Brand">
        <h:outputText value="#{meuItem.outraDescricao}" />
    </p:column>
    <p:column style="width:32px;text-align: center">
         <p:commandButton value="Download" id="blabla" actionListener="#{minhaView.fileDownload(meuItem)}">
            <p:fileDownload value="#{minhaView.file}" />
        </p:commandButton>
    </p:column>
</p:dataTable>

Reference: link              link

    
04.05.2018 / 22:29