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