FileUpload rerender
frer Aug 21, 2009 4:51 PMHi,
I'm trying to use the fileUpload to upload an image. When the user selects the image from the fileUpload, I want the file to start immediately to be uploaded. On completion, I want the fileUpload to be hidden and the image to appear (using a graphicImage) with a link to remove it (using a commandlink). The user can then use the remove image button to remove it and then the fileUpload would appear again.
Here is my code:
<s:div id="item-image-value-container">
 <rich:fileUpload id="item-image-value-upload"
 fileUploadListener="#{formAction.uploadImage}"
 maxFilesQuantity="1"
 immediateUpload="true"
 acceptedTypes="jpg, gif, png, bmp"
 addControlLabel="#{messages['org.mdarad.framework.resources.list.add']} Image"
 cancelEntryControlLabel="#{messages['org.mdarad.framework.resources.list.remove']} Image"
 rendered="#{formAction.entity.image == null}">
 <a4j:support event="onuploadcomplete" reRender="item-image-value-container" />
 </rich:fileUpload>
 <h:panelGrid columns="2" rendered="#{formAction.entity.image != null}">
 <s:graphicImage id="item-image-value-preview" value="#{formAction.entity.image.content}" style="width:48px"/>
 <a4j:commandLink id="item-image-value-remove"
 action="#{formAction.clearImage}"
 value="#{messages['org.mdarad.framework.resources.list.remove']} Image"
 reRender="item-image-value-container"/>
 </h:panelGrid>
 </s:div>
 public void uploadImage(UploadEvent event) throws Exception {
 UploadItem item = event.getUploadItem();
 //Get data from temporary created file
 File file = item.getFile();
 byte[] data = new byte[(int) file.length()];
 FileInputStream fileInputStream = new FileInputStream(file);
 fileInputStream.read(data);
 //Now update object
 org.dataisland.primitives.datatype.Blob blob = new org.dataisland.primitives.datatype.Blob();
 getEntity().setImage(blob);
 getEntity().getImage().setContent(data);
 getEntity().getImage().setContentType(item.getContentType());
 getEntity().getImage().setContentFileName(item.getFileName());
 }
 public void clearImage() throws Exception {
 getEntity().setImage(null);
 }As you can see I use the rendered attribute on the panelGrid and the fileUpload to decide which section should be hidden and which one shouldn't.
For a reason I do not understand, the render/reRender works on the commandLink but not on the fileUpload.
Could someone explain what I am doing wrong?
Thank you