Reputation: 41
I have included a PDF viewer in a webapp which works fine in Chrome.
But the web have to be compatible with IE8 or later.
And, sadly, the viewer throws an error in IE, in any version (always so friendly ...)
I'm using Primefaces 3.4.1 (Sorry, I can't change the version)
The code:
uploadedView.xhtml: (Eplanation: logic is inside a wizard in two steps; first for uploading, second for view)
<p:wizard widgetVar="wizard" flowListener="#{fileBean.onFlowProcess}" >
<p:tab id="upload" title="Upload docs">
<p:panel header="Upload a PDF">
<p:growl id="messages" showDetail="true" />
<p:panelGrid columns="2">
<p:fileUpload id="fileUploaded" value="#{fileBean.file}" mode="simple"/>
<p:commandButton actionListener="#{fileBean.upload}" value="Upload" ajax="false"/>
</p:panelGrid>
</p:panel>
</p:tab>
<p:tab id="view" title="View the uploaded doc">
<p:panel header="View here the doc">
<p:media value="#{fileBean.streamedContent}" width="100%" height="500px" player="pdf" rendered="#{fileBean.streamedContent != null}"/>
</p:panel>
</p:tab>
</p:wizard>
fileBean.java:
@ManagedBean
@SessionScoped
public class FileBean implements Serializable{
static final long serialVersionUID = 42L;
private UploadedFile file;
private String fileName;
private StreamedContent streamedContent;
private InputStream stream;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if(file != null) {
// Saving the stream after uploading
byte[] b = file.getContents();
stream = new ByteArrayInputStream(b);
stream.mark(0); //remember to this position!
streamedContent = new DefaultStreamedContent(stream, "application/pdf");
}
}
public StreamedContent getStreamedContent() throws IOException {
// Getting the stream data
if (file == null || file.getSize() == 0){
return new DefaultStreamedContent();
} else {
if (streamedContent != null)
streamedContent.getStream().reset(); //reset stream to the start position!
return streamedContent;
}
}
public String onFlowProcess(FlowEvent event) {
// Flow for wizard
if (event.getNewStep() == null || event.getNewStep().isEmpty()){
return event.getOldStep();
}
if (file == null || file.getSize() == 0) {
return "not file";
} else {
return event.getNewStep();
}
}
In Chrome, server simply displays this WARNING, but viewer works fine:
ADVERTENCIA: JSF1091: no se ha encontrado ningún tipo MIME para el archivo dynamiccontent. Para resolverlo, agregue una asignación de tipo MIME al archivo web.xml de la aplicación.
(Traslation: In summary, MIME type is missing in dynamiccontent)
But in IE, in addition to this, displays the next:
13-oct-2014 17:52:53 org.primefaces.application.PrimeResourceHandler handleResourceRequest GRAVE: Error in streaming dynamic resource. La expresión no puede ser nula
(Traslation: Expression can't be null)
And viewer throws a popup in browser:
File does not begin with '%PDF-"., etc ...
Above all, I don't understand why server is throwing an exception with IE.
Sorry for my english and ..
Greetings!
Upvotes: 0
Views: 2389
Reputation: 41
After researching, my conclussion:
--> Streaming from a Managed Bean seems to be impossible with IE.
Two alternatives:
Smart option: Create a servlet which will manage the file treaming, something like this (Look at @BalusC answer: Unable to show PDF in p:media generated from streamed content in Primefaces)
Another valid option: Firstly, we store pdf file in server and then display it just indicating the url
Greetings
Upvotes: 1