Tips & Tricks - Total size of all files in a file field

An application contains a file selection element which allows users to upload files in the browser. We now want to provide additional information about the attached files such as the file size or the number of files.

Requirements

When creating the file selection, activate the setting to allow the uploading of multiple files per data record in the properties dialog.

Moreover, the data group requires additional data fields to provide more information about the files:

  • Number of files (integer data type)

  • Total file size (integer data type)

  • File size in MB (floating-point number data type)

Implementation

A new process is created in the Processes module. This process responds to a data record being inserted or changed. This means that retrospective changes (files being added or deleted) to the data recod are also intercepted and analyzed. A Groovy script action is attached to the Data group event handler. The Groovy action will query the file information.

The following script is performed in the Groovy script action:

//Der FileUCHelper muss importiert werden
import de.uplanet.lucy.server.businesslogic.util.FileUCHelper

//nötige Informationen aus dem Datenfeld abfragen
def id= g_record["<GuidRecId>"].value /* datafield (PK) (S) ID <integer> */

def files = FileUCHelper.getFileValueHolder(g_context, "<Guid Dateifeld>", id)
def filesIt = files.getValue()

def intSize = 0
def anz = 0
filesIt.each{element ->	intSize = intSize + element.getFile().length()
	anz++
}

//Schreibe errechnete Informationen zurück in den Quelldatensatz
def conn = g_dbConnections.systemConnection

g_dbQuery.executeUpdate(conn, """
	UPDATE 
		DATAGROUP('<Guid Datengruppe>') 
	SET
		L_DATEIGROESSEGESAMT = ?,
		L_ANZAHLDATEIEN = ?,
		FLT_DATEIGROESSEINMB = ?
	WHERE
		LID = ?""") {
	setInt(1, intSize)
	setInt(2, anz)
	setDouble(3, intSize/1024/1024)
	setInt(4, id)
}

Documentation on https://docs.intrexx.com

The call of the FileUCHelp with the subsequent getValue() is required to identify the files contained in the file field. The, the variables for calculating the number of files and the file size is initialized and set to null. The loop (each) for every file found follows this. "element" stands for the single file that is queried in the following line with "geFile()". Its size in bytes is identified with ".length()". "anz++" increments the control variable that represents the number of files. Afterwards, the information is written back, which can of course also be achieved using shareShare (g_sharedState) and a subsequent data group action. With very large files, it could be the case that the total size of the files in bytes is too large for a normal integer data field. The database will output an error message in this case. In our example, the saving in MB is therefore specified as well - this results with the corresponding divisor. Now, when data records are added or changed in the application, the required data is calculated and written to the data field. This data can be displayed in a view table on an overview page, for example.