12 Replies Latest reply on Nov 29, 2011 7:02 AM by bcn

    RichFaces 4: FileUpload Clear Event

    lessonz

      So, I've tried catching the clear event from the "Clear All" button in a FileUpload component with no luck.

       

      <a4j:ajax event="clear" execute="@none" render="testPanel" />

       

      Any suggestions as to how to catch this event? Thanks.

        • 1. RichFaces 4: FileUpload Clear Event
          ilya_shaikovsky

          actually that looks like a bug http://docs.jboss.org/richfaces/latest_4_0_X/vdldoc/ do not contains such handler. so please add jira issue on that.

          • 2. RichFaces 4: FileUpload Clear Event
            bcn

            Yeah, a clear listener is urgently necessary!

            Both for the clear all as for individual clear actions.

            Is the jira issue created already?

             

            Thanks,

            Ulrich

            • 3. RichFaces 4: FileUpload Clear Event
              lessonz
              • 4. Re: RichFaces 4: FileUpload Clear Event
                bleathem

                Issue RF-10952 has been resolved.  It will be available when RichFaces 4.1.0.M3 has been released. (soon!)

                • 5. Re: RichFaces 4: FileUpload Clear Event
                  bcn

                  Hello,

                   

                  I tried the clear handler like this:

                   

                  <rich:fileUpload>

                  <a4j:ajax event="clear" listener="#{bean.clearUpload}"  />

                  </rich:fileUpload>

                   

                  public void clearUpload() {...}

                   

                  The bean method is called when Clear All is clicked as well as when an individual item is removed.

                   

                  But how can I distinguish these two cases and, in the latter, how can I find out in the bean method which item was removed?

                   

                  Thanks

                  • 6. Re: RichFaces 4: FileUpload Clear Event
                    bleathem

                    Your listener might be able to inspect the list of items cleared from the AjaxBehaviorEvent parameter.

                    • 7. Re: RichFaces 4: FileUpload Clear Event
                      bcn

                      Okay, the bean method can be

                       

                      public void clearUpload(AjaxBehaviorEvent event) {...}

                       

                      but I am unable to find out the deleted item from the event object.

                      I can get UIFileUpload from event.getSource(), but still no idea where to get the deleted file from.

                       

                      Thanks

                      • 8. Re: RichFaces 4: FileUpload Clear Event
                        bcn

                        I think it is impossible to determine which file was cleared. The parameter of the listener should be a FileUploadEvent, otherwise there is no reference to the file. The component has no "var" attribute to bind a bean variable to. One could define a f:param on the a4j:ajax tag, but to what could its value be set? I am stuck.

                        • 9. Re: RichFaces 4: FileUpload Clear Event
                          bleathem

                          Hmm, there are few other events you can tap into, but I'm curious, what's your use case for knowing which file has been cleared?

                          • 10. Re: RichFaces 4: FileUpload Clear Event
                            bcn

                            The user uploads several files, then clears only one of them. So I have to remove only that item from my list of uploaded items in the bean, not all of them.

                            • 11. Re: RichFaces 4: FileUpload Clear Event
                              bleathem

                              Having dug into this, it is apparent there are a number of problems with the clear handler of the file upload component.  I've got some working code showing how you can remove the uploaded files from your backing bean, but I had to create and resolve RF-11744 to do so.  You'll need a recent 4.1.0-SNAPSHOT to try this out, or wait for the next release (CR2).  I've included code snippets here, but you can see the code in context in the dev-examples.

                               

                              Consider the autocomplete tag: (From: autocomplete.xhtml)

                                      <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}" onclear="onclear(event)" ... >
                                          <a4j:ajax event="uploadcomplete" execute="@none" render="info" />
                                      </rich:fileUpload>
                              

                               

                              The onclear handler is a javascript method that digs the file list out of the javascript event:

                                          var onclear = function (event) {
                                              var data = event.rf.data;
                                              for (var i in data) {
                                                  var item = data[i];
                                                  removeFile(item.name);
                                              }
                              

                               

                              Then you'll need a <a4j:jsFunction> for the onclear handler to call into, and initiate the ajax call:

                                      <a4j:jsFunction name="removeFile" render="info" action="#{fileUploadBean.clearFile}">
                                          <a4j:param name="clearedFile" assignTo="#{fileUploadBean.clearedFile}" />
                                      </a4j:jsFunction>
                              

                              I'm pretty sure this can be done with a RichFaces javascript API call, but I couldn't find it off hand.

                               

                              You'll then have a backing bean (see: FileUploadBean.java) method "fileUploadBean.clearFile" that will be called once for each file removed, where the file to remove is stored in the backing bean property "clearedFile".

                               

                              This is functional, but gross - I consider it a workaround.  Would you please file a jira asking for a JSF behaviour to be added to the fileuplaod component, to make it easier to write onclear listeners?  We can address that in the 4.2 release timeframe.

                               

                              Brian

                              • 12. Re: RichFaces 4: FileUpload Clear Event
                                bcn

                                Thanks for your help!

                                 

                                JIRA entry:

                                 

                                https://issues.jboss.org/browse/RF-11746