3 Replies Latest reply on Jul 19, 2012 9:37 AM by Siva P

    Very Simple FileUpload Component

    pioug pioug Newbie

      The component rich:fileUpload is very powerful but it is bewildering to users. How can we do to have a simple

       

      <input name="fichier" type="file" >

       

      Like this :

       

      http://today.java.net/pub/a/today/2006/02/09/file-uploads-with-ajax-and-jsf.html

       

      Thanks

        • 1. Re: Very Simple FileUpload Component
          Brendan Healey Master

          I suppose you can try implementing the code in the link, but I think the author was awarded the nobel prize for that one.

          Alternatively try the tomahawk t:inputFileUpload component. It's a totally non-ajax solution, but it does work ok with

          container security and you see it everywhere (facebook etc...). JSF 2.2 comes with it's own file upload component.

           

          Regards,

          Brendan.

          • 2. Re: Very Simple FileUpload Component
            Ivan Ravin Newbie

            i did this way (RF 3.3.3):

            <h:panelGroup id="uploadInfo">
              <rich:fileUpload id="fileUpload"
                acceptedTypes="xml" immediateUpload="true"
                fileUploadListener="#{fileBean.listener}"
                listHeight="0"  maxFilesQuantity="1">
                <a4j:support event="onuploadcomplete" reRender="uploadInfo" />
              </rich:fileUpload>
              <h:outputText value="#{fileBean.uploadedName}"/>
            </h:panelGroup>

             

            1. allow only 1 file

            2. shrink frame (listHeight="0")

            3. rerender after upload

             

             

             

            • 3. Re: Very Simple FileUpload Component
              Siva P Master

              Hi,

               

              Richfile upload some times works different in diff browsers.It takes canonical path and Absolute Path in browsers. based upon you have to check it.

               

              I have shared my code for you and others .It will be useful.

               

              <rich:fileUpload   fileUploadListener="#{Bean.fileUploadListener}"

                                               maxFilesQuantity="1" 

                                              id="upload" addControlLabel="Browse...." clearAllControlLabel="Clear all"

                                              clearControlLabel="Clear" stopEntryControlLabel="Stop process" doneLabel="Success" autoclear="false"                

                                               acceptedTypes="xls,xlsx"  

                                              immediateUpload="#{Bean.autoUpload}"

                                               allowFlash="false" >

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

                                           </rich:fileUpload>  

               

               

              public synchronized void fileUploadListener(UploadEvent event) throws IOException {

                  String action = null;

                  try

                  {

                    Object dataObj = event.getSource();

                    log.debug(dataObj.toString());

                    UIComponent component = event.getComponent();

                    log.debug(component.getId());

               

                    this.forSuccess = null;

                    this.forSuccessWithExistingData = null;

                    this.forFailure = null;

               

                    UploadItem item = event.getUploadItem();

                    String name = "unnamed_attachment";

                    byte[] data = item.getData();

                    log.debug("File : '" + item.getFileName() + "' was uploaded");

                    this.uploadedData = item.getData();

                    log.debug("Filename:" + item.getFile());

                    log.debug("UploadedData:" + this.uploadedData);

                    if (item.isTempFile()) {

                      this.file = item.getFile();

                      name = item.getFileName();

                      data = item.getData();

                      String contentType = item.getContentType();

                      log.debug("\n Name:" + this.file.getName());

                      log.debug("uploaded " + name + " - length= " + (data == null ? 0 : data.length) + " tmpFile=" + this.file.getAbsoluteFile());

                      this.fileAbsPath = this.file.getAbsolutePath();

                    }

                    File file = new File();

                    file.setLength(item.getFile().length());

                    file.setName(item.getFileName());

                    file.setData(item.getData());

                    file.setMime(item.getContentType());

                    this.files.add(file);

                    this.uploadsAvailable -= 1;

               

                    Boolean tetFlag = UploadFileAction(this.fileAbsPath);

                    log.debug("return Boolean:" + tetFlag);

                    HttpServletResponse response;

                    if (tetFlag.booleanValue())

                    {

                      writeToFile(this.bufferFileExistsDBObj);

                      weiteToFileWrongCCardNoFormt(this.bufferErrorFormatObj);

               

                      log.info("FacesContextMessage.........");

                      FacesContext faceContext = FacesContext.getCurrentInstance();

                      FacesMessage facesMessage = new FacesMessage();

                      facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);

                      facesMessage.setDetail(this.ppsResources.getString("excelfileuploaded"));

                      facesMessage.setSummary(this.ppsResources.getString("excelfileuploaded"));

                      faceContext.addMessage("mainPpsInclude:uploadBlacklistModalView:blacklistedUploadModal:uploadexcel", facesMessage);

               

                      FacesContext context = FacesContext.getCurrentInstance();

                      response = (HttpServletResponse)context.getExternalContext().getResponse();

                    }

                    else

                    {

                      log.debug("Else part error.....");

                      action = "false";

                      this.forFailure = "Failure";

                    }

                    try

                    {

                      log.debug("Deleting file in temp:" + item.getFile());

                      item.getFile().delete();

                      log.debug(item.getFile() + " -File is successfully deleted..");

                      if ((this.forSuccess == null) && (this.forFailure == null) && (this.forSuccessWithExistingData == null))

                      {

                        this.forSuccess = "Nodata";

                      }

                    }

                    catch (Exception e) {

                      e.printStackTrace();

                    }

                  }

                  catch (Exception e) {

                    e.printStackTrace();

                    this.forFailure = "Failure";

                    this.hiddenStatus = "Failure";

                    setForFailure("Failure");

                  }   

                  log.debug("Before clearing merchantlistbean add......");

                }

               

               

              Thanks ,

              Siva