Primefaces filedownload does not download

0

Good afternoon, I have an application where I use Primefaces, ManagedBeans, Spring and Hibernate, I'm making a button to download a PDF file by passing the route as a parameter. Doing Debug, I see that if it executes, but the stream is returned by Null, I have been looking for other similar questions around here but they do not work, I have made sure that the path and the file exist, but it does not download the file, here my code

ManagedBean

    public void descargarFormato(ActionEvent actionEvent) throws Exception {

        String fileName = (String)actionEvent.getComponent().getAttributes().get("fileName");
        File file = new File(fileName);
        InputStream input = new FileInputStream(file);
        ExternalContext externalContext = 
        FacesContext.getCurrentInstance().getExternalContext();
        setArchivoDescarga(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));

    }

My XHTML

<h:form id="formaUpdate" enctype="multipart/form-data">
<p:commandButton value="DESCARGAR ARCHIVO" ayax="false" update="formaUpdate" actionListener="#{resguardoMB.descargarFormato}">
	<p:fileDownload value="#{resguardoMB.archivoDescarga}" />
	<f:attribute name="fileName" value="#{resguardoMB.rutaFormato}"/>
</p:commandButton>
</h:form>

Les agradezco mucho

This is the complete code of my XHTML:

<h:form id="formaUpdate" enctype="multipart/form-data">
					<p:growl id="growl" showDetail="true" sticky="true" />
					<h1>ACTUALIZAR RESGUARDO</h1>
					
					<input type="hidden" name="nombreUserLog" id="nombreUserLog" value="#{loginMB.nombre}" />
					
					<h:inputHidden id="idResguardo" value="#{resguardoMB.idResguardo}"/>
					<h:inputHidden id="rutaFormato" value="#{resguardoMB.rutaFormato}"/>
					<h:inputHidden id="nombreUserLogEdit" value="#{loginMB.nombre}"/>
					
					<p:panel header="Adicionales">
					<p:panelGrid columns="4" layout="grid" style="width: 100%" cellpadding="5" styleClass="alignLeft">
					
						<h:outputLabel for="numSeMaletaLaptop" value="Maleta Laptop " />
						<h:outputLabel for="numSeCandadoLaptop" value="Candado Laptop " />
						<h:outputLabel for="numSeVideoCam" value="Video Cámara " />
						<h:outputLabel for="numSeCamDigital" value="Cámara Digital " />
						
						<p:inputText id="numSeMaletaLaptop" value="#{resguardoMB.numSeMaletaLaptop}"></p:inputText>
						<p:inputText id="numSeCandadoLaptop" value="#{resguardoMB.numSeCandadoLaptop}"></p:inputText> 
						<p:inputText id="numSeVideoCam" value="#{resguardoMB.numSeVideoCam}"></p:inputText>
						<p:inputText id="numSeCamDigital" value="#{resguardoMB.numSeCamDigital}"></p:inputText>
						
						<h:outputLabel for="numSeVideoProy" value="Video Proyector " />
						<h:outputLabel for="numSeMemoriaUsb" value="Memoria USB " />
						<h:outputLabel for="numSeTelCelu" value="Teléfono Celular " />
						<h:outputLabel for="numSeOtroEquipo" value="Otro Equipo " />
						
						<p:inputText id="numSeVideoProy" value="#{resguardoMB.numSeVideoProy}" ></p:inputText>
						<p:inputText id="numSeMemoriaUsb" value="#{resguardoMB.numSeMemoriaUsb}"></p:inputText>
						<p:inputText id="numSeTelCelu" value="#{resguardoMB.numSeTelCelu}"></p:inputText>
						<p:inputText id="numSeOtroEquipo" value="#{resguardoMB.numSeOtroEquipo}" ></p:inputText>
						
						<h:outputLabel for="comentarios" value="Comentarios " />
						<h:outputLabel></h:outputLabel>
						<h:outputLabel></h:outputLabel>
						<h:outputLabel></h:outputLabel>
						
						<p:inputTextarea id="comentarios" rows="5" cols="30" value="#{resguardoMB.comentarios}" counter="contador" maxlength="150" counterTemplate="{0} caracteres restantes. " autoResize="false" />
    					<p:commandButton value="ACTUALIZAR" action="#{resguardoMB.updateResguardo}" update="growl"></p:commandButton>
						<p:commandButton rendered="#{loginMB.resguardo_rol eq ('SUPERADMIN' or 'ADMIN')}" value="DESCARGAR ARCHIVO" ayax="false" >
							<p:fileDownload value="#{fileDownloadView.file}" />
						</p:commandButton>
						<p:button value="INICIO" outcome="index"></p:button>
    																	
    					<h:outputText id="contador" /> 
    					<h:outputLabel></h:outputLabel>
						<h:outputLabel></h:outputLabel>
						<h:outputLabel></h:outputLabel>
						
					</p:panelGrid>
    					<p:fileUpload id="archivoFormato" rendered="#{loginMB.resguardo_rol eq ('SUPERADMIN' or 'ADMIN')}" fileUploadListener="#{resguardoMB.handleFileUpload}" mode="advanced" update="growl" multiple="false"
			                sizeLimit="1048576"
			                allowTypes="/(\.|\/)(pdf)$/"
			                uploadLabel="Cargar" cancelLabel="Cancelar" label="Buscar archivo"/>
			            <h:outputLabel></h:outputLabel> 
					</p:panel>
					</h:form>

Thanks

    
asked by Jorge Alberto López Zamarripa 09.01.2018 в 18:09
source

2 answers

0

After the tests you did, it worked for you to add to the button these lines that are ajax of primefaces onclick="PrimeFaces.monitorDownload (start, stop);" update="@ form"

<p:commandButton rendered="#{loginMB.resguardo_rol eq ('SUPERADMIN' or   'ADMIN')}" value="DESCARGAR ARCHIVO" onclick="PrimeFaces.monitorDownload(start, stop);" update="@form">
                        <p:fileDownload value="#{fileDownloadView.file}" />
                    </p:commandButton>
    
answered by 15.01.2018 / 23:19
source
0

As Raul mentioned, the problem was the ajax instructions, I was combining ayax="false" and update="formUpdate", in the end I only used update="@ form" to update the form, and I already downloaded, although Now I have another related problem, but that will go in another post, the code was like this: My XHTML

<h:inputHidden id="rutaFormato" value="#{resguardoMB.rutaFormato}"/>

<p:commandButton value="DESCARGAR ARCHIVO" actionListener="#{resguardoMB.descargarFormato}" onclick="PrimeFaces.monitorDownload(start, stop);" update="@form">
							<p:fileDownload value="#{resguardoMB.archivoDescarga}" />
						</p:commandButton>

My ManagedBean As you will see I also had to copy the file into the resources folder of my application because I did not take it from the external path

public void descargarFormato() throws Exception {

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String rutaFormatoasign = ec.getRequestParameterMap().get("formaUpdate:rutaFormato");

    File origen = new File(rutaFormatoasign);
    String nombre = origen.getName();
    File destino = new File("C:/RutaWebApp/resources/" + "archivoDescarga.pdf");

    try {
            InputStream in = new FileInputStream(origen);
            OutputStream out = new FileOutputStream(destino);

            byte[] buf = new byte[1024];
            int len;

            while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
            }

            in.close();
            out.close();
    } catch (IOException ioe){
            ioe.printStackTrace();
    }
    rutaFormato = rutaFormatoasign;
    InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/resources/" + "archivoDescarga.pdf");
    setArchivoDescarga(new DefaultStreamedContent(stream, "application/pdf", nombre));
}
    
answered by 17.01.2018 в 06:49