Automatically upload multiple images

5
Currently, my application contains a catalogue functionality. I want to enrich the catalogue with images. Since I do not have so much time, I do not prefer to upload it manually. Is there a way to automatically upload all the images (2000 .png files) at once? Perhaps I can put the images in the 'uploaded files' folder in the 'WEB-INF' folder?
asked
2 answers
6

You can place the images in another temporary folder, not in the 'uploaded files' folder. Create also a Java action which reads the files in your temporary folder. See this answer. Then create for each file (in the for-loop) a FileInputStream object, this is also an InputStream, needed for storing the image contents. Now create a new Mendix System.Image object, and store the content of the image to this object.

Example:

File folder = new File(pathname);

for(File file : folder.listFiles()) {
    // create a new System.FileDocument instance

    FileInputStream inputStream = new FileInputStream(file);

    // Call the following method and use the FileInputStream just created as parameter.
    // Core.storeImageDocumentContent(IContext context,
            IMendixObject imageDocument,
            InputStream inputStream,
            int thumbnailWidth,
            int thumbnailHeight)
}

More information about the Core.storeImageDocumentContent(...) method can be found in this answer.

answered
3

The solution of Jonathan is great. But it miss one little thing, as Michel / Fedor says: How will the application know which image should be connected to which product?

To set a relation between a product and the image they must have something in collective. For example: if the name of the product is "jacket" the corresponding image have to be "jacket.png". There have to be an analogy between the name of the image and a attribute of the product meta object.

So after executing the JAVA action given by Jonathan the rest of the job can be done by microflow:

  • Retrieve all created System.Image objects
  • For each image retrieve the product based on a corresponding attribute (e.g. the product name equals the System.Image file name)
  • Set the relation between them

I'm afraid there are currently no better options to do this.

answered