2 Replies Latest reply on Jan 17, 2011 4:32 AM by ando0098

    test on the file name <rich:fileUpload>

    ando0098 Newbie

      I want to do a test on the file name to download, for example I will only accept file name with the following format YYYYMMDDhhmmss.txt.

      I did this :

       

       

       

      <h:message id="errorFile" for="upload" class="error" />
             <rich:fileUpload
              fileUploadListener="#{MyBean.listener}"
              rendered="true" maxFilesQuantity="1" id="upload"
              immediateUpload="false" acceptedTypes="txt"
              allowFlash="false" noDuplicate="true"
              listHeight="100px">
              <f:facet name="label">
               <h:outputText value="{_KB}KB copié sur {KB}KB  {mm}:{ss}" />
              </f:facet>
       </rich:fileUpload>
      

      public String listener(UploadEvent event) throws Exception {
        String expression = "[0-9]{14}";
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(event.getUploadItem().getFileName());
        if (!matcher.matches()) {
         //Generate error message
         return "";
        }
        //My action if file name is correct
        return "sucsess";
       }
      
      


      this works fine, but it's after uploading file, i have to do this before uploading file.

      I have tried this

       

       


      But what i have to put in the value?

      <a4j:support event="onadd"
           action="#{MyBean.validateUploadItem}"
           reRender="errorFile">
           <a4j:actionparam value="?"
                assignTo="#{MyBean.uploadItem}" />
      </a4j:support>
      
      

       

      Can you help me?

      thx

       

      <h:message id="errorFile" for="upload" class="error" />
      <rich:fileUpload
            fileUploadListener="#{MyBean.listener}"
            rendered="true" maxFilesQuantity="1" id="upload"
            immediateUpload="false" acceptedTypes="txt"
            allowFlash="false" noDuplicate="true"
            listHeight="100px">
            <f:facet name="label">
               <h:outputText value="{_KB}KB copié sur {KB}KB  {mm}:{ss}" />
            </f:facet>
      </rich:fileUpload>
      
      
      

       

        • 1. Re: test on the file name <rich:fileUpload>
          Ilya Shaikovsky Master

          unfortunatelly need to admit that there is no simple way. you can't use ajax to upload the file. our fileUpload actually uses submits from iframes in order to achieve assync of upload requests. so a4j:support will not work. And with current component functionality you really has single place to validate - in uploadListener.

           

          B.t.w. if client side solution is enough - just try to handle onadd by JS. check dcumentation to read how to get access to file entry in handler and return false if it doesn't fit requirements.

           

          Let me know if will not works and I will make tests on our side.

          • 2. test on the file name <rich:fileUpload>
            ando0098 Newbie

            i did this and it works fine :

            Page:

            <h:from>

            <h:message id="errorFile" for="upload" class="error" />

            <rich:fileUpload

                  fileUploadListener="#{MyBean.listener}"

                  rendered="true" maxFilesQuantity="1" id="upload"

                  immediateUpload="false" acceptedTypes="txt"

                  allowFlash="false" noDuplicate="true"

                  listHeight="100px">

                 <a4j:support event="onadd" onsubmit="document.getElementById('hiddenId').value=$('upload').component.entries[0].fileName;"

                       action="#{MyBean.validateUploadItem}" reRender="#{MyBean.item}" />

                  <f:facet name="label">

                     <h:outputText value="{_KB}KB copié sur {KB}KB  {mm}:{ss}" />

                  </f:facet>

            </rich:fileUpload>

            <h:inputHidden value="#{MyBean.uploadItem}" id="hiddenId" />

            </h:form>

             

            Bean:

            public String validateUploadItem() {

              item="errorFile";

              if (!validate(uploadItem)) {

               item=item+",upload";

               generateErrorMessage();

              }

              return "";

            }

             

            I have a small problam, the error message didn't display.

            for this, i used javascript and i works fine

            <a4j:support event="onadd" onsubmit="document.getElementById('hiddenId').value=$('upload').component.entries[0].fileName;"

                       action="#{MyBean.validateUploadItem}" reRender="#{MyBean.item}"

                       oncomplete="if(#{MyBean.showErrorMessage}) alert(#{msg.invalid_file_name});" />

             

            thx.