2
votes

Is it possible to block/disable the "Choose" button in FileUpload?

I use the p:fileUpload in advanced mode and set multiple="true". If I click on the Upload button to start the upload of all file, I want to prevent the adding of more files until all files are uploaded.

Version of PrimeFaces is 5.1.

Definition of p:fileUpload:

<p:fileUpload id="fileUpload" update=":form:uploadTable :msg :msgs" fileUploadListener="#{fileUploadBean.handleFileUpload}" mode="advanced" dragDropSupport="false"
              multiple="true" sizeLimit="3221225472" fileLimit="100" style="margin:15px 0px;width:900px;" />


Workaround with p:blockUI
I found a workaround with using of blockUI element. So on start the blockUI is show and after all uploads are completed the blockUI is hide. For that javascript code is needed.

<p:fileUpload id="fileUpload" update=":form:uploadTable :msg :msgs" fileUploadListener="#{fileUploadBean.handleFileUpload}" mode="advanced" dragDropSupport="false"
                  multiple="true" sizeLimit="3221225472" fileLimit="100" style="margin:15px 0px;width:900px;"
                  onstart="setUploadFilesCount()" oncomplete="handleUploadComplete()" />  
<p:blockUI block="fileUpload" widgetVar="wVarBFileUpload" />

Javascript Code:

<script type="text/javascript">
    var fileCounter;
    var numberOfFiles;
    function setUploadFilesCount() {
        PF('wVarBFileUpload').show();
        fileCounter = 0;
        numberOfFiles = $('.ui-fileupload-preview').size();
    }

    function handleUploadComplete() {
        fileCounter++;
        if(fileCounter == numberOfFiles) {
            PF('wVarBFileUpload').hide();
        }
    }
</script>
1
Isn't it possible with onstart and oncomplete attributes and some Javascript ?Fidan Hakaj

1 Answers

5
votes

You can use onstart and oncomplete to achieve this:

<p:fileUpload onstart="disableChoosing()" 
              oncomplete="enableChoosing()"
              widgetVar="uploadWV"/>

<script>
   function disableChoosing() {
     PF('uploadWV').disableButton(PF('uploadWV').chooseButton);
     PF('uploadWV').chooseButton.find('input[type="file"]').attr('disabled', 'disabled');
   }

   function enableChoosing() {
    if(!PF('uploadWV').files.length) {
        PF('uploadWV').enableButton(PF('uploadWV').chooseButton);
        PF('uploadWV').chooseButton.find('input[type="file"]').removeAttr('disabled');
    }
   }
</script>