If you’re using the community commons module at all I suggest having a look at the DuplicateFileDocument java.
They’re using something like this to get the contents and re-store them again:
InputStream inputStream = Core.getFileDocumentContent(context, toClone);
try {
Core.storeFileDocumentContent(context, target, (String) toClone.getValue(context, system.proxies.FileDocument.MemberNames.Name.toString()), inputStream);
return true;
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(inputStream != null)
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
You can use similar logic for the reading, you’ll need something a bit different for writing though.
You could try to change the Body attribute of your Attachment entity to a unlimited String to see whether it is not empty then. Probably you can then use the Base64 decode to get the binary back into a Mendix file document.
Update: we “solved” it by changing the “Body” attribute into a string-type.
If we ever do get a java-action to work which allows to set a binary attribute we’ll let you know :)
Thanks! Yeah I don’t think I have to write anything to a FileDocument, though obviously I might be wrong :) I did use a community commons class to figure out the second argument of the “setBody”-method.
Small update: I did try to set the third argument of the “setBody”-method (the length of the inputstream) to the fileSize + 1, which returns an end of file error. Also I tried some small random values > 0, which all worked. Meaning the inputstream does seem to read the correct data, which is not-null.
Any other ideas? Maybe simply setting the attribute (“setBody”) is not enough and we need a refresh or a commit?
If anyone knows a way of checking the value of a binary attribute in debug-mode that would be a great help as well.
Using the debugmode in Eclipse, I did find out the actual value appears to be null, thanks! :)
Will continue to work on this later this week, maybe I can make a Java-action more people can use (FileDoc to binary or something).