Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay uploaded images before store to disk
    primarykey
    data
    text
    <p>Greetings to everyone,</p> <p>I am using primefaces 4 and Tomcat 7. I want users to be able to upload multiple images and see each uploaded image instantly (while they are in memory), before these are written to the disk. The images will only be written in the disk after form submission. I am using p:fileUpload component. </p> <p>Here is the relevant code:</p> <pre><code>... &lt;p:tab id="imageTab" title="#{msgs.images}"&gt; &lt;p:dataGrid id="imagesDataGrid" columns="4" value="#{modifyProductAdminBean.imageIds}" var="imgId" &gt; &lt;p:graphicImage value="#{pA_ImageService.image}" &gt; &lt;f:param name="id" value="#{imgId}" /&gt; &lt;/p:graphicImage&gt; &lt;/p:dataGrid&gt; &lt;p:fileUpload fileUploadListener="#{modifyProductAdminBean.handleFileUpload}" mode="advanced" dragDropSupport="true" multiple="true" sizeLimit="5242880" invalidFileMessage="#{msgs.invalidFileType}" invalidSizeMessage="#{msgs.fileTooLarge}" allowTypes="/(\.|\/)(gif|jpe?g|png|jpg)$/" cancelLabel="#{msgs.cancel}" uploadLabel="#{msgs.upload}" label="#{msgs.choose}" update="imagesDataGrid" /&gt; &lt;/p:tab&gt; ... </code></pre> <p><br/></p> <pre><code>@ManagedBean @ViewScoped public class ModifyProductAdminBean implements Serializable { private Map&lt;String, UploadedFile&gt; uploadedImages; public void handleFileUpload(FileUploadEvent event) { UploadedFile file = event.getFile(); String uniqueId = UUID.randomUUID().toString(); this.getUploadedImages().put(uniqueId, file); } public Set&lt;String&gt; getImageIds() { return this.getUploadedImages().keySet(); } public Map&lt;String, UploadedFile&gt; getUploadedImages() { return uploadedImages; } ... } </code></pre> <p><br/></p> <pre><code>@ManagedBean @ApplicationScoped public class PA_ImageService implements Serializable { private final ModifyProductAdminBean modifyProductAdminBean; public PA_ImageService() { this.modifyProductAdminBean = BeanManager.findBean("modifyProductAdminBean"); } // Taken from http://stackoverflow.com/questions/8207325/display-image-from-database-with-pgraphicimage public StreamedContent getImage() { FacesContext context = FacesContext.getCurrentInstance(); if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL. return new DefaultStreamedContent(); } else { // So, browser is requesting the image. Return a real StreamedContent with the image bytes. String imageId = context.getExternalContext().getRequestParameterMap().get("id"); // remove [, ] characters between imageId = imageId.substring(1, imageId.length() - 1); UploadedFile uFile = this.modifyProductAdminBean.getUploadedImages().get(imageId); return new DefaultStreamedContent(new ByteArrayInputStream(uFile.getContents())); } } ... } </code></pre> <p><br/></p> <pre><code>public class BeanManager implements Serializable { @SuppressWarnings("unchecked") public static &lt;T&gt; T findBean(String beanName) { FacesContext context = FacesContext.getCurrentInstance(); return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class); } ... } </code></pre> <p>When I run this code I get a NullPointerException at the last line of “PA_ImageService” (return new ...). More precisely, although uFile is not null “uFile.getContents()” returns null. Why? What am I doing wrong?</p> <hr> <p>More details that I observed: </p> <p>I noticed that when I upload a file, Tomcat stores it temporarily inside <code>E:\Program Files (x86)\Apache Software Foundation\Apache Tomcat 7.0.41\work\Catalina\localhost\MyProject</code> directory in a <code>.tmp</code> file.</p> <p>By debugging the project, I can see that: When I reach the <code>if (context... == PhaseId.RENDER_RESPONSE)</code> line of <code>PA_ImageService</code>, the .tmp file still exists. However, in the second access of <code>getImage()</code> method, when the control moves to the <code>else</code> block, I can see that the tmp file no longer exists. Therefore, its contents cannot be retrieved and hence the <code>null</code> result.</p> <p>Any ideas of how this is happening? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload