14 Replies Latest reply on Apr 6, 2016 4:30 AM by Jose Antonio Sánchez

    Problem using rich:fileUpload..listener not working..

    debashis Malo Newbie

      Hi,

       

      Having problem with rich:fileUpload fileUploadListener. Corresponding listerner in the backing bean is not working.

       

      This is very urgent. Need immediate help..

       

       

      Partial code is given below.

       

      <rich:fileUpload width="100%" fileUploadListener="#{fileSourceConfigBean.fileUploadBean.listener}"
           maxFilesQuantity="#{fileSourceConfigBean.fileUploadBean.uploadsAvailable}" id="srcFile"
           immediateUpload="#{fileSourceConfigBean.fileUploadBean.autoUpload}"
           acceptedTypes="#{fileSourceConfigBean.fileUploadBean.acceptedTypes}"
           allowFlash="#{fileSourceConfigBean.fileUploadBean.useFlash}"
           addControlLabel="Browse....">
           <a4j:support event="onuploadcomplete" reRender="info" />
      </rich:fileUpload>

       

      ------------------------------------------------------------------------------------------

      The listener defined in FileUploadBean is as follows.

       

      public void listener(UploadEvent event) throws Exception{
            UploadItem item = event.getUploadItem();
           String FullfileName = item.getFileName();
           String fileName=FullfileName.substring(FullfileName.lastIndexOf("\\")+1);

           log.info("File Name :" + fileName);
              ExternalContext con = FacesContext.getCurrentInstance().getExternalContext();
              ServletContext sCon = (ServletContext) con.getContext();
              //String filepath = sCon.getRealPath(relativePath);
              String filepath = sCon.getRealPath("/");
             
              log.debug("Real Path :"+filepath);
              log.info("Real File name with Path :"+filepath+fileName);
              setFileNameWithPath(filepath+fileName);
              //log.info("Real Path1 :"+filepath1);
              File file = new File();
              try{
               java.io.File uploadFile = new java.io.File(filepath, fileName);
               //log.info("uploadFile.getPath():::::" + uploadFile.getPath());
                  if(!uploadFile.exists()){
                   //log.info("inside !uploadFile.exists()");
                      FileInputStream fis = new FileInputStream(item.getFile());
                      //log.info("After 1");
                      //FileOutputStream out = new FileOutputStream(uploadFile);
                      FileOutputStream out = new FileOutputStream(filepath+fileName);
                      //FileOutputStream out = new FileOutputStream(fileName);
                      //log.info("After 2");
                      int bytes = 0;
                      byte[] bteFile = new byte[(int)event.getUploadItem().getFile().length()];
                      //log.info("Before 3");
                      while ((bytes = fis.read(bteFile)) != -1) {
                               out.write(bteFile, 0, bytes);                        
                      }               
                      out.close();
                      file.setLength(event.getUploadItem().getFile().length());
                      file.setName(item.getFileName());
                      file.setData(bteFile);
                      file.setUploadedFile(uploadFile);
                      getFiles().add(file);
                      uploadsAvailable--;
                  }           
              } catch(Exception ex){
               log.info("Exception Caught in fileUpload:" + ex.getMessage());
               ex.printStackTrace();
                 
              }

          }

       

       

      Regards,

      Debashis

        • 1. Re: Problem using rich:fileUpload..listener not working..
          Ilya Shaikovsky Master
          what did you mean by "not working".. Is it called but works wrong? Or not called at all?
          • 2. Re: Problem using rich:fileUpload..listener not working..
            debashis Malo Newbie

            Hi,

             

            The listener is not called at all.

             

             

            Regards,

            Debashis

            • 4. Re: Problem using rich:fileUpload..listener not working..
              debashis Malo Newbie

              Attached is my web.xml

               

               

              Regards,

              Debashis

              • 5. Re: Problem using rich:fileUpload..listener not working..
                Ilya Shaikovsky Master
                seems looks fine. Ok.. then if the FU component enclosed with form component?
                • 6. Re: Problem using rich:fileUpload..listener not working..
                  Lisa DeSouza Newbie

                  I have the same problem, I've replicated the UI and the FileUploadBean and File beans from the demo pagehttp://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf. and my web.xml is attached.

                   

                  I'm using:

                  • SWF 2.0.3
                  • RichFaces 3.3.3

                   

                  I have the following problems:

                  1. If I use the rich:fileUpload component with allowFlash="false", it shows "Transfer error"  (I'm fine with using flash, so this can be ignored for now)
                  2. So I'm using allowFlash="true"; and now the temp files are automatically uploaded into my tomcat temp folder, even though I have the listener configured to handle them.

                   

                  Please help!!

                  • 7. Re: Problem using rich:fileUpload..listener not working..
                    Lisa DeSouza Newbie

                    Oops, ok looks like my problem wasn't due to the component... i had nested forms on my page. Once I changed that, the listener calls. Issue 1 however, still persists.

                    • 8. Problem using rich:fileUpload..listener not working..
                      anilmalali2010 Newbie

                      I am also facing the problem with rich:fileUpload ..listener . I have googled for the solution but didnt find the solution . I am trying to test the code given in

                      demo pagehttp://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf. But compiler is essuing an error . The stack trace is as bellow

                       

                      org.apache.jasper.JasperException: /fileupload.jsp(21,51) #{..} is not allowed in template text     org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)     org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)     org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:102)     org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:713)     org.apache.jasper.compiler.Node$ELExpression.accept(Node.java:958)     org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)     org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)     org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:840)     org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1530)     org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)     org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)     org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:840)     org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1530)     org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)     org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)     org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)     org.apache.jasper.compiler.Node$Root.accept(Node.java:495)     org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)     org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1736)     org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:183)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:332)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)     org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)     com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:414)     com.sun.faces.application.ViewHandlerImpl.executePageToBuildView(ViewHandlerImpl.java:455)     com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:139)     org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)     org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)     com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:108)     com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:266)     com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:159)     javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)     org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)     org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)     org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)     org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)

                       

                      The JSP code where i am getting error is

                       

                      <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}" maxFilesQuantity="#{fileUploadBean.uploadsAvailable}" id="upload" immediateUpload="#{fileUploadBean.autoUpload}"

                      acceptedTypes="jpg, gif, png, bmp" allowFlash="#{fileUploadBean.useFlash}">

                                      <a4j:support event="onuploadcomplete" reRender="info" />

                                  </rich:fileUpload>

                       

                      Help Please ...!!! Thanks in Advance

                      • 10. Re: Problem using rich:fileUpload..listener not working..
                        linus55 Newbie

                        Hi, I also have problems with rich:fileUpload calling listener. Here's my code snippet:

                         

                                   <rich:fileUpload id="file"

                                            required="true"

                                            immediateUpload="true"

                                            validator="#{detailsView.validateFileUploaded}"

                                            maxFilesQuantity="1"

                                            listHeight="59px"

                                            styleClass="requiredField"

                                            style="background-color:red; border:none; width: 100%; "

                                            addControlLabel="#{detailsView.createDocument ? translations.fileAddFile : translations.fileExchangeFile}"

                                            cleanButtonClass="hide"

                                            cleanButtonClassDisabled="hide"

                                            stopEntryControlLabel="#{translations.fileStopUpload}"

                                            clearControlLabel="#{translations.fileClear}"

                                            uploadControlLabel="#{translations.fileUploadControl}"

                                            uploadButtonClass="hide"

                                            uploadButtonClassDisabled="hide" 

                                            binding="#{detailsView.fileUploadComponent}"

                                            fileUploadListener="#{detailsView.fileUploadListener}" />

                         

                        However, fileUploadListener is not called. My web.xml is attached. Alternatively, I added attribute uploadData="#{detailsView.uploads}" where uploads is a Collection in my backing bean. But neither getter nor setter method is called.

                         

                        My Configuration:

                        - JBoss PortletBridge 2.1.0

                        - RichFaces 3.3.3.FINAL

                        - IBM WebSphere Portal Server 6.1.0.5

                         

                        Thanks in advance!

                        • 11. Re: Problem using rich:fileUpload..listener not working..
                          Henrique Oliveira Newbie

                          I had the same problem using the seam and RichFaces 2.2.2.Final 3.3.3.final

                          My problem was with some component or page structure, which made the requestParameterMap come without the parameter field in the upload process decode the page.

                          Below is the snippet of code (org.richfaces.renderkit.FileUploadRendererBase) where the "problem" happened.

                           

                           

                          protected void doDecode(FacesContext context, UIComponent component) {
                              UIFileUpload fileUpload = (UIFileUpload) component;
                              String clientId = component.getClientId(context);
                              ExternalContext externalContext = context.getExternalContext();
                              Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
                          
                              if (requestParameterMap.get(clientId) != null) { //here is null
                                  String actionString = requestParameterMap.get(FileUploadConstants.FILE_UPLOAD_ACTION);
                          
                                  String uid = requestParameterMap.get(FileUploadConstants.UPLOAD_FILES_ID);
                          
                                  if (actionString != null) {
                                      new UploadAjaxActionEvent(component, actionString, uid).queue();
                                  }
                          
                                      String fileUploadIndicator = requestParameterMap.get(FileUploadConstants.FILE_UPLOAD_INDICATOR);
                                      if (fileUploadIndicator != null && Boolean.TRUE.toString().equals(fileUploadIndicator)) {
                                          decreaseFileCounter(context, clientId);
                          
                                          MultipartRequest multipartRequest = MultipartRequest.lookupRequest(context, uid);
                          
                                          boolean isFlash = (requestParameterMap.get("_richfaces_send_http_error") != null);
                          
                                          List<UploadItem> fileList = multipartRequest.getUploadItems();
                          
                                          if (fileList == null || fileList.size() == 0) { 
                                              return;
                                          }
                          
                                          boolean formUpload = multipartRequest.isFormUpload();
                          
                                          if (isFlash && !formUpload && fileList.size() > 0) {
                                              try {
                                                  UploadItem item = fileList.get(0);
                                                  int actualSize = item.getFileSize();
                                                  int clientSize = Integer.parseInt(requestParameterMap.get("_richfaces_size"));
                          
                                                  if (actualSize != clientSize) {
                                                      return; // File uploading has been stopped on the client side
                                                  }
                                              }catch (Exception e) {
                                                  return;
                                              }
                                          }
                                          onUploadComplete(context, fileList, fileUpload, formUpload);
                                      } //jump to the end of method withou call onUploadComplete where the event broadcast is raised
                              }
                          
                          
                          }
                          
                          

                           

                          After changing my page (not sure exactly what) the component started to function properly.

                          • 12. Re: Problem using rich:fileUpload..listener not working..
                            manpd15 Newbie

                            Hi ,  I am facing same issue.i am using the file uploadin modal popup. I am attaching the file.

                            i tried it with disabling flash but it is giviing " transfer error".

                            • 13. Re: Problem using rich:fileUpload..listener not working..
                              Vishwajit Shinde Newbie

                              Hi ,

                               

                              My code also didnt calling listener method. When I removed form tag then it was working.

                               

                              Thank you,

                              • 14. Re: Problem using rich:fileUpload..listener not working..
                                Jose Antonio Sánchez Newbie

                                I had the same problem, and the solution was "<h:form>" instead "<jf:form>"