Knowledge Base for DX 7 : Set Maximum Filesize for File Upload Element Through Bean Shell Validator

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.

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/1000000 + ") | " + errorMessage);
	result = false;
}

return result;


Attachments: