Excel upload (new topic)

Hello everyone.

I’m considering to migrate an ETL application from CUBA to JMIX and I have this issue:

I need to upload an Excel spreadsheet, to open it and to launch data processes according to the data it contains.

In CUBA, I was using a FileUploadField and was getting a File object thanks to the FileUploadAPI. This file was then converted in FileInputStream, required to work with Apache POI.

As far as I’m seeing, this isn’t possible with JMIX anymore: it looks like we do need to use entities to store documents in a storage

  1. it is not what I want: is there another solution that doesn’t impose the file to be stored ?

  2. I had hard time to get a FileInputStream from the upload. I’m using the code below, but I don’t find it elegant. Is there any better option ?

      FileRef fr = storedDc.getItem().getFile();
      InputStream fi = fileStorage.openStream(fr);
      File file = new File("data.xlsx");
      try (FileOutputStream outputStream = new FileOutputStream(file)) {
          int read;
          byte[] bytes = new byte[1024];
          while((read = fi.read(bytes)) != -1) {
              outputStream.write(bytes,0, read);
          }
      }
      FileInputStream fis = new FileInputStream(file);
    

Thanks in advance. Have a nice day.

O.

If I understand the task correctly, you can use ‘FileStorageUploadField’.

Set a property to manually process the file.
<fileStorageUpload id="fileField" fileStoragePutMode="MANUAL"/>

And process after uploading. I haven’t tested, but it should work…

@Subscribe("fileField")
public void onFileFieldFileUploadSucceed(SingleFileUploadField.FileUploadSucceedEvent event) throws FileNotFoundException {
        File file = temporaryStorage.getFile(fileField.getFileId());
        FileOutputStream outputStream = new FileOutputStream(file);
        //Use POI
        temporaryStorage.deleteFile(fileField.getFileId());
    }
1 Like

This is working perfectly. Many thanks.