0 Replies Latest reply on Sep 18, 2019 10:20 AM by redhatchittak

    rich:fileUpload in JSF version 3.3.3 final, 1 file uploaded causes file upload listener to run twice in Firefox v69

    redhatchittak

      For some reason, best guess is something todo with how broswer handles the ajax event a single fileupload triggers the event listener in Java twice.

       

      The problem is worst in Firefox.

       

      Latest Chrome version and in old Firefox v28(2014) general work.

       

      Size of files uploaded is small < 1MB around 160kb

       

      I have tried changing to none ajax <h:commandButton this clearly creates two post requests in the browser console with 5 bytes difference in the http content-length header.

       

      Objective is to upload and process a single file: - restrict it to a single file upload - check to see if the uploaded file is valid XML - save the uploaded file in another location which a newly generated name - delete the temp file

       

      Richfaces FileUpload side:

      <a4j:form id="formFileUpload">
        
      <h:panelGrid id="fileUploadSection" columns="1" width="100%">

        
      <!-- ##### File uploader ##### -->
        
      <!-- https://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/rich_fileUpload.html -->
        
      <rich:spacer width="10px" height="0px" />
        
      <h:outputText value="Upload file candidate:" />
        
      <rich:fileUpload fileUploadListener="#{attributeConfigurationMgr.validateFileUploadListener}"
        
      addControlLabel="Select File..."
        
      id="upload"
        
      immediateUpload="true"
        
      allowFlash="false"
        
      noDuplicate="true"
        
      listHeight="55px"
        
      onupload="console.log('fileupload onupload:'+Date.now())"
        
      oncomplete="console.log('fileupload complete:'+Date.now())"
        
      >
        
      <a4j:support event="onuploadcomplete" ajaxSingle="true" reRender="validatePanel, validationFilesTable, fileUploadSection"  />
        
      </rich:fileUpload>
        
      </h:panelGrid>
        
      </a4j:form>

       

      Java side event listener:

      As 2 events get generated I have tried to identified check that the same temp uploaded file isn't being processed to avoid double processing, it seems through maybe the temp upload file isn't always fully there.

      Is their a way to detect the file upload header size? Then I could check to see if the temp file is the correct size or not?

      private String lastUploadedFile = null;

      public synchronized void validateFileUploadListener(final UploadEvent event) {

      UploadItem item = event.getUploadItem();
      File uploadedFile = item.getFile();
      String uploadedFileName = item.getFileName();

        
      if (lastUploadedFile != null && lastUploadedFile.contentEquals(uploadedFileName))
        
      {
        
      // check doesn't it have the same file upload name
        firstEvent
      = false;
        
      }
        
      else
        
      {
        
      // set name as hopefully first upload event
        lastUploadedFile
      = uploadedFileName;
        firstEvent
      = true;
        
      }

      if(Files.exists(uploadedFile.toPath())) {
      // find XSD file to validate against
      // validate against xsd
      // save file
      // temp file delete
      }

      }