I am facing a problem where I want to store the exact size of an uploading file in a variable so that I can access it later to validate even if the section containing the file upload field is hidden. I am on v19.1. PS. By size, I mean the exact size in bytes and not "<1KB" which is retrieved by using fv!files.size. I cannot use a!save() function in validation attribute and fv!files.size is not accessible outside validation.
Discussion posts and replies are publicly visible
Once you upload the file, later in the process you can use the document function to get the size of the file.
The file is currently stored in a RuleInput. My concern is that if the file is of 0 bytes, I will allow the user to navigate to another section of the form but cannot let him submit the form. In this use case, there can be more than just a single file upload field on different-different sections and only the 0-byte file needs to be stopped and not 1 or more bytes file. Please let me know if I am clear.
If I understand correctly, you should still be able to apply the document function in this scenario. Once a document reference is stored in a variable (even if it's not a document type - use todocument()), you can use document(<documentvariable>,"size") to return a numerical representation of the size of the file and compare that to 0.If you save the the result of that operation, you can use it in the evaluation of a showWhen, or in a validation.
I tried it but the thing is document function cannot be used on a document which isn't completely uploaded and stored on Appian but only a reference of it exists. It shows as error "Either the document is missing or has been deleted"
That's correct. When do you need the size? Just after uploading the file or after submitting the form? In case you need after the uploading the file, you can do a work around on this. After uploading the file, provide a soft submit button which will submit the form and revert back to the same form, make use of chaining so the form will get submitted and reopen the form, by doing this your document will get uploaded to the KC folder and you will be able to get the size.
The things that you need to make sure is the values that the user have entered already, that should retain after clicking the soft submit button.
If you do not want to resort to submitting your form just to fetch the file size, there is one way to handle this all while still being on the interface. What you have to do is to create an expression rule and pass the document as a rule input. Use below code in your exp rule,
local!fileSize:a!fileUploadField_17r1(value:ri!document).contents.size
This will return you the size of the file which has just been uploaded and yet to be saved in the KC. Use this exp rule to conditionally submit form based on your use case.
P.S.: I am using older version of file upload because .contents does not work with new version. One probable (mostly obvious) reason could be the change in property names. But Appian hasn't deprecated the old version yet since 17.2, so we can use it. Maybe they will allow us to access file name, size, extension, etc in the saveInto parameter in the future.
Hope this helps!
Take care! Access to the size attribute appears to be different in 19.2 (a change that has obviously occurred since 17.1):
local!fileSize:a!fileUploadField(value: ri!myDocument).contents.value.fileSizeDisplay
(if the file is <1KB you still don't get the actual fielsize)
If it's just a matter of not letting files of a certain size be uploaded, then the current OOTB functionality should be fine for you since you can access the file sizes directly within the "validations" parameter of a!fileUploadField.
(Edit: as I noted in a different reply below, "fv!files.size" contains the file sizes in bytes, NOT the "display value" i.e. "<1KB".)
a!fileUploadField( value: ri!doc, saveInto: ri!doc, maxSelections: 1, validations: { if( fv!files.size = 0, "Empty documents not allowed.", null() ) } )
Unknown said:By size, I mean the exact size in bytes and not "<1KB" which is retrieved by using fv!files.size.
This is not true. fv!files.size accesses the file size in bytes. You're thinking of the "display size" which is what says "<1KB".