How to delete image filedocument content

2
I have an object that derives from Image, and thus filedocument. I have created a new instance of this object and uploaded a file. Then I save this object, the content is filled and a thumbnail is created. Now, I would like to delete the content, but keep the object instance. How can I do this?
asked
2 answers
4

One (kinda dorkie) way to do this would be to simply overwrite the filedocumentcontents with an empty string:

com.mendix.core.Core.storeFileDocument(getContext(), myMendixObject, new ByteArrayInputStream("".getBytes()));

the other (nicer) way of doing it is:

    File origFile = com.mendix.core.Core.getFileDocumentContentAsFile(fil.getMendixObject());
    String path = origFile.getAbsoluteFile().getParent() + File.separator + "thumbs" + File.separator + fil.getFileName();
    File thumbFile = new File(path);
    thumbFile.delete();
    origFile.delete();

However, the problem with the second solution is that I suspect that they'll deprecate that method soon, because it's generally nicer to work with inputstreams than with file objects directly.

(PS: I have only updated the second solution for getting rid of thumbnails, I don't think the first solution will get rid of thumbs)

answered
3

My advice: don't do it.

The idea of a FileDocument object is that it represents a file on disk. Removing the object means removing the file on disk. The same holds for creating and changing a FileDocument object.

If you want to have an object containing additional information, do not derive from FileDocument but create an association to FileDocument. It that way you can upload new documents by creating a new FileDocument and setting the association.

The same story holds for an Image object. Do not inherit from Image but create an association to it. An image object represents and image (and optionally its thumbnail) on disk. Actions like create, remove, etc. should act on both items.

answered