Constructing a FileDocument from java code (problem)

I am having this code to retrieve a set of xls files from a directory and translate them to a Mendix FileDocument. But there seems to be going something wrong when I want to call the flow of the import excel module. It goes wrong at this part of the code, It seams when constructing the FilDocument the filename disappears. //Determine file format for header extraction int lastdot = fileName.lastIndexOf("."); if (lastdot < 0) throw new CoreException("Found file has no extension to derive format from."); public class RetrieveFileFromPath extends UserAction<list<imendixobject>> { private String ImportPath; public RetrieveFileFromPath(String ImportPath) { super(); this.ImportPath = ImportPath; } @Override public List<IMendixObject> executeAction() throws Exception { // BEGIN USER CODE List<IMendixObject> returnFileDocs = new ArrayList<IMendixObject>(); File dir = new File(this.ImportPath); File[] files = dir.listFiles(); if(files != null && files.length > 0) { for(int index=0; index < files.length; index++) { InputStream inputStream = new FileInputStream(files[index]); IMendixObject mendixObject = Core.create(this.getContext(), "System.FileDocument"); Core.storeFileDocumentContent(getContext(), mendixObject, inputStream); returnFileDocs.add((IMendixObject) mendixObject); } } return returnFileDocs; // END USER CODE } /** * Returns a string representation of this action */ @Override public String toString() { return "RetrieveFileFromPath"; } // BEGIN EXTRA CODE // END EXTRA CODE } If i am doing something wrong let me know.
2 answers

There is a storeFileDocumenContent method that takes the filename as a string parameter, it would have no way of knowing the filename from the inputstream itself.

Can you tell what it is that is going wrong? Do you get an exception, does the filename not show up, will the file not load?

By the way, I would not recommend throwing a CoreException since this is what is used by the runtime (business server) itself. In this case I would probably throw a UserException


Two things. First of all, you can pass the name of the file to the core api as well:

public static void storeFileDocumentContent(IContext context, IMendixObject fileDocument, String fileName, InputStream inputStream)

secondly, you pasted a little bit of code to determine the lastIndex of ".", but you're not using any of that you your java action? If you have a file object you can always call getName on it, btw.