简体   繁体   中英

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

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.

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
}

}

I don't know the inner workings of RF 3 all that well, but:

If this is browser related I'm assuming the form is being submitted twice. If that's the case you should be able to intercept that. FileUpload.upload is a public method, the general form-submitting method appears to be _JSFFormSubmit . You can override one of those methods and providing you tell apart the duplicate cancel the execution.

If you cannot detect things on the client side you can intercept server requests by implementing a javax.servlet.Filter (the same way RichFaces does, using org.ajax4jsf.webapp.BaseFilter ). That way you'll have access to request headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM