1 2 Previous Next 15 Replies Latest reply on Apr 25, 2011 12:56 AM by Lisa DeSouza

    rich:fileUpload - removing uploaded files

    Prasad Kolas Newbie

      Hi,
      Has anyone managed to remove a uploaded file using rich:fileUpload. The clear or remove JS methods just remove the file from the client list. Already uploaded files are not touched.

      Is there a way of capturing which file is being removed and accordingly remove it on the server side?
      Cheers

        • 1. Re: rich:fileUpload - removing uploaded files
          Daniel Yang Novice

          Hi, I think you want to use a4j:jsFunction, it can accomplish what you want, just pass the file name you want to delete, then get it on the server side and do deletion.

          • 2. Re: rich:fileUpload - removing uploaded files
            Prasad Kolas Newbie

            Hi, thanks for the reply. Do you have a working example somewhere. I was not able to pass the filename or id around to the server.

            • 3. Re: rich:fileUpload - removing uploaded files
              Ilya Shaikovsky Master

              call the function defined with jsFunction like this.

              onclear="myFunc(event.memo.entry.fileName);"

              then add f:param with name = "fname" without value to jsFunction.

              so parameter with filename will be substituted automatically as it shown at demo. And you will be able to delete the file you need from server.

              Just do not forget to handle situation when cancel called (before upload) cancel and remove fires the same event.

              • 4. Re: rich:fileUpload - removing uploaded files
                Prasad Kolas Newbie

                Thanks for your help. I have got it working.

                • 5. Re: rich:fileUpload - removing uploaded files
                  Ralph Soika Apprentice

                  Hello,

                   

                  can you please provide an example code to fit things together. I can not figure out how this should work.

                  My JSF Code looks currently like this:

                   

                  {code:xml}

                       <rich:fileUpload  listWidth="345px;" fileUploadListener="#{fileUploadBean.listener}"
                              maxFilesQuantity="#{fileUploadBean.uploadsAvailable}" id="upload"
                              immediateUpload="#{fileUploadBean.autoUpload}"
                              allowFlash="false">
                              <a4j:support event="onclear"
                                  actionListener="#{fileUploadBean.clearUploadData}" >
                                  <a4j:actionparam name="fname" assignTo="#{fileUploadBean.fileName}" />
                              </a4j:support>           
                          </rich:fileUpload>

                  {code}

                   

                  When I click on the "clear" link of an uploaded file my action method .clearUploadData is called. Thats fine.

                  But I can not figure out how to get the filename inside my actionListener method. So I will not know which of the files in the list should be removed by my backend method

                   

                  I think my actionparam is nonsense but I can not find the right way to get the filename provided in my ActionEvent.

                   

                  Thanks for any help

                  Ralph

                  • 6. Re: rich:fileUpload - removing uploaded files
                    Ilya Shaikovsky Master

                    please check my suggestion above once more. I've talked about jsFunction and not a support. It allow to call ajax as just simple JS method abnd pass parameters just to registered function. Also check richfaces-demo for jsFunction component. There is simple usage and params passing shown.

                    • 7. Re: rich:fileUpload - removing uploaded files
                      Ralph Soika Apprentice

                      Thanks for your response.

                      My misunderstanding was the way how to bind the param with the filename to my backing bean.

                      My code first looks like this:

                       

                      <rich:fileUpload onclear="clearOneFile(event.memo.entry.fileName);"  listWidth="345px;" fileUploadListener="#{fileUploadBean.listener}"
                                  listHeight="100px"
                                  maxFilesQuantity="#{fileUploadBean.uploadsAvailable}" id="upload"
                                  immediateUpload="#{fileUploadBean.autoUpload}"
                                  xacceptedTypes="jpg, gif, png, bmp"
                                  allowFlash="false">
                              </rich:fileUpload>
                                  <a4j:jsFunction name="clearOneFile"  actionListener="#{fileUploadBean.clearUploadData}">
                                        <a4j:actionparam  name="param1" assignTo="#{fileUploadBean.fileName}"/>
                                 </a4j:jsFunction>

                       

                      So I have expected that I can access the property fileUploadBean.fileName inside my actionListener method. But I recognized that the setter method for the property was called after my action listener method. So this was my problem to get out the selected filename.

                      But I found a solution to get the param out from the faces context.

                      So my actionListener method looks now like this:

                       

                      public void clearUploadData(ActionEvent event) {
                               FacesContext context = FacesContext.getCurrentInstance();
                               String fileName = context.getExternalContext().getRequestParameterMap().get("param1").toString();
                             
                               // remove file form backing bean.....
                      }

                       

                      Now my code works. Finnaly the assignTo property is obsolete in this situation - is this the right way?

                       

                      thanks

                      Ralph

                      • 9. Re: rich:fileUpload - removing uploaded files
                        Ralph Soika Apprentice

                        Thanks a lot again.!

                        now its perfect after I moved the actionListener into the actionparam tag:

                         

                        <rich:fileUpload cleanButtonClass="fileuploadClearButton" clearAllControlLabel="" onclear="clearOneFile(event.memo.entry.fileName);"  listWidth="345px;" fileUploadListener="#{fileUploadBean.listener}"
                                    listHeight="100px"
                                    maxFilesQuantity="#{fileUploadBean.uploadsAvailable}" id="upload"
                                    immediateUpload="#{fileUploadBean.autoUpload}"
                                    allowFlash="false">           
                                </rich:fileUpload>
                                    <a4j:jsFunction name="clearOneFile" >
                                          <a4j:actionparam  name="param1" assignTo="#{fileUploadBean.fileName}" actionListener="#{fileUploadBean.clearUploadData}"/>
                                   </a4j:jsFunction>

                         

                         

                         

                        One last question:

                        When I add the onclear event like I do now, the "Clear all" button is no longer available throght an actionListener. the reason seams to be that the onclear function call now disables the possibility to work additioal with <an a4j:even name="onclear"..... />

                        This is the reason wy hide the cleanAllControlLabel and the cleanButton. (?)

                         

                        ralph

                        • 10. Re: rich:fileUpload - removing uploaded files
                          Ralph Soika Apprentice

                          After all I found a solution to handle both cases in my backend bean. The case if the user clears only one of the uploaded files as also the case if he clicks the 'clear all' button.

                           

                          My rich:fileuplaod looks now like this:

                           

                          <rich:fileUpload listWidth="345px;" fileUploadListener="#{fileUploadBean.listener}"
                                      maxFilesQuantity="#{fileUploadBean.uploadsAvailable}" id="upload"
                                      immediateUpload="#{fileUploadBean.autoUpload}"
                                      allowFlash="false">   
                                      <a4j:support event="onclear">
                                         <a4j:actionparam  name="fname" noEscape="true" value="(event.memo.entry)?event.memo.entry.fileName:'' "
                                             assignTo="#{fileUploadBean.fileName}"
                                             actionListener="#{fileUploadBean.clearUploadData}"/>
                                      </a4j:support>
                                  </rich:fileUpload>

                           

                           

                          I use the a4j:support on the "onclear" event to call my backing actionListener (for both cases)

                          Additional I provide an a4j:actionparam where I test if one file was selected (event.memo.entry.filename is set) or if the "clear all" button was pressed (event.memo.entry is undefined).

                          In my backingBean I now test if the filename property of my backing bean was set to a filename or if this value is ''. In the second case I can clear all files from my backing bean.

                          This is the code of my actionListener method:

                           

                          public void clearUploadData(ActionEvent event) {
                                  try {
                                      // test if a single file was cleared....
                                      if (fileName != null && !"".equals(fileName)) {
                                          System.out.println("Removing single fileName=" + fileName);
                                          filesUploaded.remove(fileName);

                                          ..........

                                      }

                                      else {
                                          // remove all files form fileUploadBean...
                                          Iterator<String> iter = filesUploaded.iterator();
                                          System.out.println("Removing all files....");
                                          while (iter.hasNext()) {
                                              String s = iter.next();
                                              System.out.println("Removing fileName=" + s);
                                              .......
                                          }
                                          filesUploaded.clear();
                                      }
                                  } catch (Exception e) {
                                      e.printStackTrace();
                                  }
                              }  

                           

                           

                          Now the component works greatly with my backing bean implementation

                          Thanks again for support

                           

                          Ralph

                          • 11. Re: rich:fileUpload - removing uploaded files
                            Ilya Shaikovsky Master

                            nice to hear that finally works! B.t.w I'm planning to add your solution to knowledgebase. https://jira.jboss.org/jira/browse/RFPL-529 If you feel that have some simple solutions which you could share - please efeel free to update our community knowledgebase wiki pages also

                            • 12. Re: rich:fileUpload - removing uploaded files
                              Ralph Soika Apprentice

                              Hi,

                              I have also written a blog about the usage in my example in more detail. Maybe this will be helpful. But the implementation depends on the Imixs Workflow components - especially in the way the files will be stored into the database. But the code of the FileuploadBean is posted in this blog.

                               

                              http://www-02.imixs.com/roller/imixsworkflow/entry/richfaces_fileupload

                              • 13. Re: rich:fileUpload - removing uploaded files
                                Lisa DeSouza Newbie

                                That's all fine, I used your solution perfectly, till someone mentioned, once the Add button gets disabled by maxFilesQuantity, it doesn't get enabled again, even after clear/clear All.... HELP!!

                                1 2 Previous Next