Leandro Alsina
Leandro Alsina

Reputation: 193

Error trying to download content with PrimeFaces <p:fileDownload> java.io.IOException: Error in streaming dynamic resource Exception

Hello fellow programmers. I'm tryin to use <p:fileDownload> in a JSF app with PrimeFaces 10. I want to download a plain data file (not a graphic or image), which is fetched from the database as a byte array.

In the view I have the download button:

<h:form enctype="multipart/form-data">
...
    <p:commandButton value="Descargar..." 
                     rendered="#listaMaterialesBacking.listaSeleccionada.nombreArchivoDiseno != null}">
        <p:fileDownload value="#{listaMaterialesBacking.descargaDiseno}"/>
    </p:commandButton>
</h:form>

In the (view scoped) Managed bean I have:

@Named(value = "listaMaterialesBacking")
@ViewScoped
public class ListaMaterialesBacking implements Serializable {
...
    public StreamedContent getDescargaDiseno()
    { 
        //we fetch the byte array from the db
        byte[] archivoD = getListaMaterialesFacade().recuperarArchivoDiseno(listaSeleccionada.getId()); 
        
        StreamedContent file = DefaultStreamedContent.builder()
                                .name(listaSeleccionada.getNombreArchivoDiseno())
                                .contentType("application/octet-stream")
                                .stream(() -> new ByteArrayInputStream(archivoD))
                                .build();
    
        return file;
    }
}

Then when I click in the download button, the browser replies with a 15KB file download that has inside HTML code than contains inside:

Error processing request Context Path: /obrasttt-1.0-SNAPSHOT

Servlet Path: /faces

Path Info: /javax.faces.resource/dynamiccontent.properties

Query String: ln=primefaces&v=10.0.0-RC1&pfdrid=471e3b498e52f03eee44242c8050ee23&pfdrt=sc&pfdrid_c=false&uid=62d196e9-84c2-455e-81b7-c089f53b80f6

Stack Trace:

java.io.IOException: Error in streaming dynamic resource at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:120) ...

Files stored in the DB are not very large, these are like 500kB size.

I'm using:

Primefaces 10.0.0-RC1.jar

JDK 1.8

JSF 2.3 (mojarra)

Wildfly 25

MySQL 5.7

Apache NetBeans 12.4

What should I do? Thanks in advance

Upvotes: 2

Views: 1567

Answers (1)

Jasper de Vries
Jasper de Vries

Reputation: 20263

You are using Ajax downloading, which uses dynamic content streaming, which does not support @ViewScoped.

For a working example, see the showcase.

Upvotes: 2

Related Questions