How to check an attachment type and size before upload?

Hi,

I would like to know if its possible to check the file type (extension) and size of an attachment before it gets put into the Leap App. I have noticed as soon as you add an attachment into a Form, when you select a file and navigate to the next field the attachment gets uploaded to the Leap App immediately, and not on submit. We need to check the file type (is it a image) and its size (say a limit of 1MB).

Regards
Damien

I think it is not possible. I can't see any size related attribute in JavaScript, neither before submit nor after submit.

Hi there,

i second this question. On the attachment object a function is needed to make validations against at least the file type and file size. I did create a ticket on this and would suggest you do the same.

The general approach is that the file validation is performed at the server level. We have existing properties that define what file types are allowed and how large attachments can be.

attachmentFilesWhiteList - defines the file types that can be attached

attachmentFilesMaxSize - defines the maximum size of an attachment

Now, if you are asking to have different requirements at the app level then that is a feature request.

It is true that the attachment is uploaded directly to the server.

You could use javascript to get the file type and size. When a file is uploaded, the attachment object contains the id, uid and fileName. You could derive the type from the extension of the fileName. If you don't trust that, then you could make a call to our rest api to get the actual attachment.

https://opensource.hcltechsw.com/leap-doc/9.3.7/ref_data_rest_api_retrieve_attachment.html

You could then look at the headers to get the content-type and size.

I agree that we should consider making this easier, but it would be really helpful to us if you provide a use case.

For me it seems straightforward to enhance the object returned by F_Attachment.getValue() to provide also file size and MIME type.

It is good to have some server-scoped technical limit and whitelist but an obvious business case is to control attachments at application level.

Hi there,

i opened a ticket for that. What would be needed in my opinion is the following:
- reduce the selectable file types in dialog (e.g. restrict to .jpg, .png, .gif or .pdf)
- verify a selected file for it's size before it is uploaded (e.g. 10MB max)
- this should be configurable on a attachment object level (field a is for PDF with max 10mb, field b is for image with max 1mb)

FYI here is my AHA idea here https://domino-ideas.hcltechsw.com/ideas/VOLT-I-284 from January 2023

you have my vote. It's unfortunate that this was still not reviewed yet by hcl in AHA.

I have added my upvote too. Because the attachment is uploaded to the server immediately, without a preupload check the Leap database could get populated with alot of bad files.

Hi,

So I found something I can use to check extension:

In the onItemChange() event I used this. It marks the field invalid but I can't seem to remove the attachment when its bad.

var o = null; //object
o = BO.F_Attachment1.getValue();
var uploadedFileName = o.fileName;
if (uploadedFileName === "") {
    BOA.setValid(true);
}
else {
    var fileSplit = uploadedFileName.split('.');
    var fileExt = '';
    if (fileSplit.length > 1) {
        fileExt = fileSplit.pop();
    }
    if (fileExt === "xlsx") {
      BOA.setValid(true);
    }
    else {
      BOA.setValid(false, "Not a Excel Spreadsheet file");
      BO.F_Attachment1.setValue({uid:'', id:'', fileName:''}); /// Does not Work to clear attachment
  }
}

The config settings attachmentFilesWhiteList , attachmentFilesMaxSize look to be global across all Leap apps, so while helpful does not help when I need one type of attachment to be images only and limited to 1MB, but another be an Excel spreadsheet which could be 2MB+ in size. The size limits are imposed by a third party that we have to collect files and upload to them (along with other data).

I'm not sure if my brain can handle calling a REST api within the javascript callbacks (does the Java security policies allow that?)... so I might have to go with the run a Service Agent tricks and supporting database things I have been using up to now, which means manual triggering of checks.

Regards
Damien

We do not allow you to call setValue() to clear an item's value from inside of onItemChange, because that would cause an endless loop. If you encounter a condition where you need to clear the attachment's value, then you need to do it from a different context.

For example, one approach is to move the validation code to the onBlur event (of the attachment item) and then you could use setValue() to clear it's value.