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

    Empty UploadItem when using fileUpload

    t1mb0

      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..

        • 1. Re: Empty UploadItem when using fileUpload
          akakunin

          I have same problem then I', trying to place fileUpload into modalPanel.

          Problem solved in my case if I (that is strange!) moving fileUpload component out of the form.

          Try to move it outside form - may be this will help

          • 2. Re: Empty UploadItem when using fileUpload

            No ideas why the same code works on demo but does not in your application.

            Only one thing to suggest: rebuild and clear cache in browser if you tried file upload before GA release. (There are some changes in JS with CR).

            The last thing: to provide us by your war file to inspect and debug.

            • 3. Re: Empty UploadItem when using fileUpload
              t1mb0

              To further this issue I can see that fileUpload is simply not working correctly when used in a new Seam project.

              I can do the following to reproduce the issue with fileUpload:-

              1. Generate a new seam project, using Seam Gen in JBoss Tools using Seam 2.0.1.

              2. Swap in the RichFaces 3.2.0 jars

              3. Import fileupload code from exadel live demo and place UI control on home page in a form.

              4. Deploy to JBoss 4.2.2 using container RI jsf jars

              5. Upload a file, the listener does fire however the UploadItem contains null attributes.


              I have seen other posts about the Seam multipart filter stopping the listener from firing when using MyFaces. This scenario is using the reference implementation and the listener does fire.

              I hope some of the RichFaces developers can have a look at this in conjunction with the the other posts. I do have a very simple ear file I could attach, (just tell me where to put it)

              Thanks,

              Tim

              • 4. Re: Empty UploadItem when using fileUpload
                t1mb0

                Can anybody help with this? I'm really keen to find a solution and can provide a test case so that it can be reproduced.

                If anyone can help I'd be really grateful,

                Thanks

                Tim

                • 5. Re: Empty UploadItem when using fileUpload
                  bneuman

                  Just for fun, change the createTempFiles parameter to true and see what item.getFile() does for you.

                  • 6. Re: Empty UploadItem when using fileUpload
                    t1mb0

                    Thanks for the response.

                    However, changing the property to true make no difference. The listener still fires and the UploadItem attributes are all null.

                    Regards,

                    Tim

                    • 7. Re: Empty UploadItem when using fileUpload

                      You cab create issue in JIRA and attach your EAR file there.

                      But before please try Richfaces 3.2.1 SNAPSHOT for test.

                      It likes that the problem is in JSF version implementation.

                      This problem has been fixed in RF 3.2.1 and currently fileupload behavior does not more depend on JSF implementation.


                      • 8. Re: Empty UploadItem when using fileUpload
                        t1mb0

                        Hi Andrei,

                        Thanks for the response:-

                        I have rebuilt using RF 3.2.1 snapshot and can confirm that this has improved the situation. When the listener fires, a temp file has been created and I can read it.

                        A new problem exists, in that the following init-params are being ignored:-

                         <filter>
                         <display-name>RichFaces Filter</display-name>
                         <filter-name>richfaces</filter-name>
                         <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>1000</param-value>
                         </init-param>
                         </filter>
                         <filter-mapping>
                         <filter-name>richfaces</filter-name>
                         <servlet-name>Faces Servlet</servlet-name>
                         <dispatcher>REQUEST</dispatcher>
                         <dispatcher>FORWARD</dispatcher>
                         <dispatcher>INCLUDE</dispatcher>
                         </filter-mapping>
                        


                        I should not have a temp file and the max file size should be enforced (but this does not happen.)

                        Would you like me to raise a JIRA for this?

                        Thanks,

                        Tim

                        • 9. Re: Empty UploadItem when using fileUpload
                          nbelaevski

                          Tim,

                          Yes, please.

                          • 10. Re: Empty UploadItem when using fileUpload
                            t1mb0
                            • 11. Re: Empty UploadItem when using fileUpload

                              Hi Tim,

                              Please difene Ajax4Jsf's parameters inside the Seam filter to fire your application.
                              I wrote example in the issue that you created.

                              This is a necessary thing when you use RF together with Seam, because Seam Framework performs auto filters configuration. So definition of Ajax4Jsf in wex.xml has no effect on application work.

                              • 12. Re: Empty UploadItem when using fileUpload
                                t1mb0

                                thanks for the info Andrei.

                                Now when I look at the seam docs it does say that Seam initialises rich faces. I guess thats RTFM!

                                Cheers

                                Tim