Last changed date of a file

In the Mendix modeler it is possible to upload a file with the file manager. Now I would like to know some info from the uploaded file, for example: The last changed date of a PDF document. On upload I would like to store this in an attribute of the Mendix object that derives from FileDocument. It is possible to retrieve this info when using the file uploader? This could be very handy for document management. I noticed that there is a modification attribute in the System.Document object, however this is attribute is always empty. Maybe it is possible to store the last changed date of the file in this attribute.
2 answers

Yes that's possible, but not with microflow of the file uploader. Futhermore the System.Document/ModificationDate is not used by the uploader.

So you need a Java action to get the last modified data from a File.

You can call the Java action on (for example) the after create event of your class that inherits from System.FileDocument. Pass the created object as parameter to the Java action. The Java action has to contain at least the following instructions:

File javaFile = Core.getFileDocumentContentAsFile(fileDocument)
long lastModified = javaFile.lastModified();

Click here for more information about the lastModified function. The lastModified function returns a long witch can be converted to a Date. For more information about that conversion see my answer on this question (click).


As far as I know you can't read the lastmodified date of arbitrary files in java. However, if you really wanted to, you could try and parse the PDF file using the pdfbox library (which is included with the mendix runtime) and use that to read the lastmodified date.

Inputstream mypdf = Core.fetchdocumentblabla
PDDocument doc = PDDocument.load(content);

Obviously, this would only work for PDFs, but it might help.