Java Code Uploading to fileDocument

Hi, It has been suggested that I use a java action to upload files to Mendix. Does anyone have any code that will allow me to do this. I am trying to upload files to my server using ftp and then load them in to Mendix and then allocate each one to a Menidx Object. Not a JAVA programmer so not really sure how to do the java bit? Thanks Nick
2 answers

Not being familiar with Java makes it tricky, but you're never too old to learn!

Mendix has its own API for communicating with the runtime but all in all, Java is still Java. No need for any special Mendix Java knowledge, just plain old Java, so we can easily jump on Google for some quick tutorials.

First, we need to watch a folder for changes.

Watching a directory

This one already covers a lot of ground, at the end we end up with the newly added file.

After we've found a new file, we want to create a matching new FileDocument in Mendix to store it in.

IMendixObject fileDoc = Core.create(context, FileDocument.entityName);

Make sure you use the 'FileDocument' from system.proxies for this. Proxies are Java representations of your Modeler entities and let you manipulate them.

Now all we have to do is create a new inputstream from the original file and combine it with the new filedocument using the function you found earlier.

FileInputStream input = new FileInputStream(file);
Core.storeFileDocumentContent(context, fileDoc, input);
Core.commit(context, fileDoc);

Deleting the file to clean up afterwards (make sure you check if creating/setting/committing the filedocument succeeded) is specified in this tutorial.

Deleting a file


See my example in