You are viewing limited content. For full access, please sign in.

Question

Question

Need Java Trigger to Calculate File Count After File Uploaded

asked on January 16, 2020 Show version history

We have a file upload button and are trying to count the files uploaded and store them in a field that will determine other required fields based on whether files are uploaded or not. We can get the count of the files uploaded we just can't trigger it in a way that seems efficient. The current code that works:

  //On draft upload. We update the file count and need a more efficient yet reliable trigger.

	  var draftDocCount=0;
      
       $('#q29').bind('DOMSubtreeModified', function (e) {
        if (e.target.innerHTML.length>0){
          draftDocCount=$('#q29 .file-del').length; //get file count
        }
        $('#q205 input').val(draftDocCount);//store file count in another field
        });

This code works but triggers anywhere from 13-16 times for each upload. This seems to slow down the upload speed. Is there a better trigger?

The classic triggers ie. change and blur trigger before the attachment is uploaded resulting in a number that doesn't include the currently uploading attachment.

The solution must also trigger on a file delete to update the total files.

0 0

Answer

SELECTED ANSWER
replied on January 21, 2020

I would just add a second check to handle the deletions:

 

$(document).on("fileuploadvalidate", "#q29", function (e) {
  $('#q205 input').val($('#q29 .file-del').length);
});

$(document).on("click", ".deleteFileBtn", function (e) {
  $('#q205 input').val($('#q29 .file-del').length);
});

There is probably a cleaner or more consolidated and efficient way to do this but unfortunately I only had limited time to play.

Hope it helps.

3 0

Replies

replied on January 17, 2020

There is an event handler called fileuploadvalidate that may be more efficient for your use. 
Note: Forms does not call fileuploadvalidate in preview mode so you will need to publish in order to test.  

$(document).on("fileuploadvalidate", "#q29", function (e) {
  $('#q205 input').val($('#q29 .file-del').length);
});

This will accurately show the current count for uploaded files, anything that is still uploading will not be counted until the upload completes (validate is called).  Hope that helps.

5 0
replied on January 17, 2020

Hello,

 

you can check if a file is added to the upload button file in the following way : 

 

 $('.fileUploadClass').on('change','.fileuploader',function(){
    
    alert('ok');
//do stuff here
    
  });

it will run only when a file is uploaded, but not on deletion.

Please explain a little bit your target, maybe we can find another way.

Hope it helped.

Maher,

replied on January 17, 2020

That works very well. Thanks so much!

 

Any tips on my last part of the ask...how to trigger that to recalculate if one of the files are deleted?

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.