How to reduce the Maximum Upload file size to less than 1Mb

0
Hi I have developed an application where users can upload image files of their purchase reciepts. But as I have seen in the uploader settings, you can only limit a maximum upload file size to 1Mb. Is there any way to reduce the maximum file size to less than 1Mb, say may be in Kbs??   Please help Dilan
asked
1 answers
1

Dilan,

From the modeler the restriction is in MB's only. The value for the restriction is an int32 so the minimum is 1 Mb.

You can however let the user upload a file that is restricted to 1 Mb (as set in the modeler), then with a java action check the filesize after it has been uploaded and based on the maximum size that you want either accept the file or delete the file and return a message to the user.

For the java code just perform a Core.getFileDocumentContent this will return an InputStream.

In the Extra code section add something similar to the code below:

  public static byte[] getBytes(InputStream is) throws IOException {

    int len;
    int size = 1024;
    byte[] buf;

    if (is instanceof ByteArrayInputStream) {
      size = is.available();
      buf = new byte[size];
      len = is.read(buf, 0, size);
    } else {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      buf = new byte[size];
      while ((len = is.read(buf, 0, size)) != -1)
        bos.write(buf, 0, len);
      buf = bos.toByteArray();
    }
    return buf;
  }

Call this method to get the bytearray returned, and then with the length method of the byte array determine the size

The length is returned in bytes so divide by 1024 to get your Kb size.

You could then return the size to your microflow and base decisions on the outcome.

Hope this helps in finding a solution for your challenge

 

answered