Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this article, we are going to show you how to check multiple files and total up their file size so that it will not exceed a certain threshold through Bean Shell validator.

Sample Screenshots:-

This is an example where you want to upload a file that exceeds 5 MB.


This is an example where all the files are below 5 MB but they total up to exceed 5 MB which causes the validator to work.



To do this, go to a file upload element and select bean shell validator and insert the code below into script.

Code Block
import org.joget.apps.form.service.FormUtil;
import org.joget.commons.util.FileManager;

boolean result = true;
String[] values = FormUtil.getElementPropertyValues(element, formData);
long totalSize = 0;
long maxSize = 5000000;
String errorMessage = "";

for (String value : values) {
File file = FileManager.getFileByPath(value);
if (file != null) {
totalSize += file.length();
errorMessage += file.getName() + "(" + file.length()/1048576 + ") " + " | ";
}
}
if (totalSize > maxSize) {
String id = FormUtil.getElementParameterName(element);
formData.addFormError(id, "Max size(MB) (" + maxSize/1048576 + ") | " + errorMessage);
result = false;
}

return result;