12 Replies Latest reply on Apr 14, 2008 5:58 PM by Tim Goodchild

    Empty UploadItem when using fileUpload

    Tim Goodchild Newbie

      I've been pulling my hair out all day trying to get the fileUpload to work for RichFaces 3.2.0 with facelets 1.1.14.

      I practically copied the exadel live demo but in the listener method I get an UploadItem from the UploadEvent, where the bytes, file and filename attributes are all null.

      I just can't see what I'm doing wrong:-

      web.xml :

       <filter-class>org.ajax4jsf.Filter</filter-class>
       <init-param>
       <param-name>createTempFiles</param-name>
       <param-value>false</param-value>
       </init-param>
       <init-param>
       <param-name>maxRequestSize</param-name>
       <param-value>1000000</param-value>
       </init-param>
      


      xhtml form:-

       <h:form id="userProfilePhotoUpload">
       <h:panelGrid columns="2" columnClasses="top,top">
       <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"
       maxFilesQuantity="#{fileUploadBean.uploadsAvailable}"
       reRender="table" id="upload"
       immediateUpload="#{fileUploadBean.autoUpload}"
       acceptedTypes="jpg, gif, png, bmp">
       <a4j:support event="onuploadcomplete" reRender="info" />
       </rich:fileUpload>
       <h:panelGroup id="info">
       <rich:panel bodyClass="info">
       <f:facet name="header">
       <h:outputText value="Uploaded Files Info" />
       </f:facet>
       <h:outputText value="No files currently uploaded"
       rendered="#{fileUploadBean.size==0}" />
       <rich:dataGrid columns="1" value="#{fileUploadBean.files}"
       var="file" rowKeyVar="row">
       <rich:panel>
       <h:panelGrid columns="2">
       <a4j:mediaOutput element="img" mimeType="image/jpeg"
       createContent="#{fileUploadBean.paint}" value="#{row}"
       style="width:100px; height:100px;" />
       <h:panelGrid columns="2">
       <h:outputText value="File Name:" />
       <h:outputText value="#{file.name}" />
       <h:outputText value="File Length(bytes):" />
       <h:outputText value="#{file.length}" />
       </h:panelGrid>
       </h:panelGrid>
       </rich:panel>
       </rich:dataGrid>
       </rich:panel>
       <rich:spacer height="3"/>
       <br />
       <a4j:commandButton action="#{fileUploadBean.clearUploadData}"
       reRender="info, upload" value="Clear Uploaded Data"
       rendered="#{fileUploadBean.size>0}" />
       </h:panelGroup>
       </h:panelGrid>
       </h:form>
      


      FileUploadBean.java:-
      public class FileUploadBean
      {
      
       private ArrayList<File> files = new ArrayList<File>();
       private int uploadsAvailable = 5;
       private boolean autoUpload = false;
      
       public int getSize()
       {
       if (getFiles().size() > 0)
       {
       return getFiles().size();
       }
       else
       {
       return 0;
       }
       }
      
       public FileUploadBean()
       {
       }
      
       public void paint(OutputStream stream, Object object) throws IOException
       {
       stream.write(getFiles().get((Integer) object).getData());
       }
      
       public void listener(UploadEvent event) throws IOException
       {
       UploadItem item = event.getUploadItem();
       File file = new File();
       file.setLength(item.getData().length);
       file.setName(item.getFileName());
       file.setData(item.getData());
       files.add(file);
       uploadsAvailable--;
       }
      
       public String clearUploadData()
       {
       files.clear();
       setUploadsAvailable(5);
       return null;
       }
      
       public ArrayList<File> getFiles()
       {
       return files;
       }
      
       public void setFiles(ArrayList<File> files)
       {
       this.files = files;
       }
      
       public int getUploadsAvailable()
       {
       return uploadsAvailable;
       }
      
       public void setUploadsAvailable(int uploadsAvailable)
       {
       this.uploadsAvailable = uploadsAvailable;
       }
      
       public boolean isAutoUpload()
       {
       return autoUpload;
       }
      
       public void setAutoUpload(boolean autoUpload)
       {
       this.autoUpload = autoUpload;
       }
      
      }
      


      facesconfig:-
       <managed-bean>
       <managed-bean-name>fileUploadBean</managed-bean-name>
       <managed-bean-class>net.xxxx.FileUploadBean</managed-bean-class>
       <managed-bean-scope>session</managed-bean-scope>
       </managed-bean>
      


      If you can see whats going wrong here I'd be really grateful..