Skip to main content
Nintex Community Menu Bar
Solved

Nintex Form attachment size validation and control

  • August 3, 2017
  • 24 replies
  • 220 views
  • Translate

Forum|alt.badge.img+1
  • 3 replies

Hi everyone,

 

We are thinking about how to separate the attachment file size from SharePoint size imitation. For example, The SharePoint maximum upload file size is 50 MB but I need to control the attachment size to 5MB in Nintex Form. However, I cannot find any OOTB method to control the file size. Can anyone suggest other ways to achieve it?

Best answer by emha

I guess you mean Mark Altman‌'s script..

I've tried that script and noticed there is missing one closing brace there.

as well check for case when there is not any file attached needed little extend.

finally I reverted what value is returned in what case so that it is easier to use in validation rules

try following version

function IsFileSizeExceeded(maxSizeInMB) {
    var isValid=false;
    for ( var i=0; i<NWF$('input[type=file]').length; i++ )  {
        if(NWF$('input[type=file]')[i].files.length>0 && NWF$('input[type=file]')[i].files[0]!==null && NWF$('input[type=file]')[i].files[0]!==undefined) {
             if(NWF$('input[type=file]')[i].files[0].size>(maxSizeInMB*1048600)) {
                var sizeInMB = Math.round(NWF$('input[type=file]')[i].files[0].size / 10486) / 100; // # MB with two decimal places
                isValid=true;
                alert("The maximum file size is "+maxSizeInMB+"MB, but the file " + NWF$('input[type=file')[i].files[0].name + " is " + sizeInMB + "MB. Please reduce the file size before uploading.");
                break;
            }
        }
    }
    return isValid;
}

I used it as follows

View original
Did this topic help you find an answer to your question?

24 replies

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • August 3, 2017

Forum|alt.badge.img+1
  • Author
  • 3 replies
  • August 4, 2017

Thanks for your reply, I had read this discussion before. However, when I put ‌ code to my Nintex 2016, the form cannot be opened and stuck in the loading icon. 

Can anyone suggest another way to deal with it? Thanks

Translate

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • Answer
  • August 4, 2017

I guess you mean Mark Altman‌'s script..

I've tried that script and noticed there is missing one closing brace there.

as well check for case when there is not any file attached needed little extend.

finally I reverted what value is returned in what case so that it is easier to use in validation rules

try following version

function IsFileSizeExceeded(maxSizeInMB) {
    var isValid=false;
    for ( var i=0; i<NWF$('input[type=file]').length; i++ )  {
        if(NWF$('input[type=file]')[i].files.length>0 && NWF$('input[type=file]')[i].files[0]!==null && NWF$('input[type=file]')[i].files[0]!==undefined) {
             if(NWF$('input[type=file]')[i].files[0].size>(maxSizeInMB*1048600)) {
                var sizeInMB = Math.round(NWF$('input[type=file]')[i].files[0].size / 10486) / 100; // # MB with two decimal places
                isValid=true;
                alert("The maximum file size is "+maxSizeInMB+"MB, but the file " + NWF$('input[type=file')[i].files[0].name + " is " + sizeInMB + "MB. Please reduce the file size before uploading.");
                break;
            }
        }
    }
    return isValid;
}

I used it as follows

Translate

Forum|alt.badge.img+1
  • Author
  • 3 replies
  • August 7, 2017

Thanks @marian hatala. You save me.

Translate

Forum|alt.badge.img+8
  • 60 replies
  • August 24, 2017

This script is for 1mb, where or how change a 15mb for example???

Thanks

Federico MS.

Translate

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • August 24, 2017

max allowed size is determined by input paramter of IsFileSizeExceeded() function.

Translate

Forum|alt.badge.img+8
  • 60 replies
  • August 24, 2017

Thanks.

FMS.

Translate

Forum|alt.badge.img+3
  • 17 replies
  • September 26, 2018

This works great. but it seems that it validate the size of each uploaded file.

in my case I need to know the total of all uploaded files in one attachment control. the reason is to attach them in emails. 

any help?. I'm no good with JS.

kindly, vote for this update: Validate the max size of attachment control – Customer Feedback for Nintex 

Translate

Forum|alt.badge.img+6
  • 38 replies
  • February 12, 2019
Hi Emha,

This code is blocking the saving of an attachment in IE. How to over come from this?

Regards,
Hema
Translate

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • February 13, 2019

the code is one and half year old. a lot changed in the forms functionality and and its internal structures since then.

so it's of no surprise it doesn't work anymore ...

 

 

try following, this worked for me on forms v2.11.5.0 

 

function IsFileSizeExceeded(maxSizeInMB) {
     var isValid = false;
     for (var i = 0; i < NWF$('input[type=file].nf-attachmentFileInput').length; i++) {
        if (NWF$('input[type=file].nf-attachmentFileInput')[i].files.length > 0  && NWF$('input[type=file].nf-attachmentFileInput')[i].files[0] !== null  &&  NWF$('input[type=file].nf-attachmentFileInput')[i].files[0] !== undefined) {
            if (NWF$('input[type=file].nf-attachmentFileInput')[i].files[0].size > (maxSizeInMB * 1048600)) {
                var sizeInMB = Math.round(NWF$('input[type=file].nf-attachmentFileInput')[i].files[0].size / 10486) / 100; // # MB with two decimal places
                isValid = true;
                alert("The maximum file size is " + maxSizeInMB + "MB, but the file " + NWF$('input[type=file].nf-attachmentFileInput')[i].files[0].name + " is " + sizeInMB + "MB. Please reduce the file size before uploading.");
                break;
            }
        }
    }
    return isValid;
}

 

Translate

I used the code and it seems to work fine with one caveat. It attaches one less attachment after the form is valid. Basically, it does not attach the last attachment even if that attachment is valid.

 

Anyone else ran into this issue? 

 

Is there any other approach I can use to validate the attachment size?

 

 

Translate

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • February 26, 2019

I cannot reproduce the issue. for me all the attachments are saved.

 

have you tried original code or one recently updated?

have you customized the code anyhow?

 

do you test it with a different form version?

Translate

Thank you for your reply. I really appreciate it.

My forms version is Version: 2.6.0.0. I have used the previous code, the one you posted on page #1. Code works like a charm. The only challenge is as soon I use that code I miss one attachment.

I did make some changes to the code but I also tried using your code as is but it was the same result.

Here is the modified code that I used.

function IsFileSizeExceeded(maxSizeInMB) {
var isValid=false;
var totalSize=0;
for ( var i=0; i<NWF$('input[type=file]').length; i++ ) {
if(NWF$('input[type=file]')[i].files.length>0 && NWF$('input[type=file]')[i].files[0]!==null && NWF$('input[type=file]')[i].files[0]!==undefined) {
totalSize = totalSize + Number(NWF$('input[type=file]')[i].files[0].size);
} 
} 
if(totalSize > (maxSizeInMB*1048600)) {
isValid=true;
}
return isValid;
}

If the code works for you then it could be my version of Nintex Forms. In that case, anything else you can suggest? 

Translate

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • February 26, 2019

have you tried with newer code?

Translate

  • 1 reply
  • February 27, 2019

Even I am facing the same issue..it seems to be browser specific. The code works perfectly in Chrome but the last attachment gets removed when tried in IE.

Translate

I agree.

 

This code works completely fine and attachments show up properly when using the Chrome but do not work with Edge and IE.

 

I used the code above but that code does not work at all but likely because my Forms version is older. 

 

Is there any other way instead of using the validation? 

 

 

 

 

Translate

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • February 27, 2019

I'm not sure what might be different in that old version...

 

have you checked developer console whether there are not reported any errors? are you able to debug the code?

 

I'm affraid I cannot help further.

 

Translate

@emha Cheers buddy.  You were very helpful so far. Appreciate all the inputs.

 

@Vaish If you find any workaround please share. I am going to try a pure jQuery route and see if it helps.

Translate

It seems that I must have done something wrong with the new script provided by @emha 

 

Just updated the new script and this time it worked as expected. Sharing my modified script in case if anyone wants to use it. This script checks all attachments size as oppose to each attachment.

 

function IsFileSizeExceeded(maxSizeInMB) {

var isValid = false;
var totalSize = 0;     
for (var i = 0; i < NWF$('input[type=file].nf-attachmentFileInput').length; i++) {
 if (NWF$('input[type=file].nf-attachmentFileInput')[i].files.length > 0 && NWF$('input[type=file].nf-attachmentFileInput')[i].files[0] !== null && NWF$('input[type=file].nf-attachmentFileInput')[i].files[0] !== undefined) {
    totalSize = totalSize + Number(NWF$('input[type=file].nf-attachmentFileInput')[i].files[0].size);        
    }
}

if (totalSize > (maxSizeInMB * 1048600)) {                
                isValid = true;
}

Thank you very much @emha 

This works on all three browsers i.e. Edge, Chrome and IE.

Translate

Forum|alt.badge.img+14
  • Scholar
  • 4681 replies
  • February 27, 2019

great!

 

just for reference, do you say this works on 2.6.0?

Translate

Yes. Works on 2.6.0
Translate

Forum|alt.badge.img+3
  • 17 replies
  • April 28, 2019

This script was working great. but after updating nintex forms i noticed that it's not working properly.

the form is not uploading any files. it shows blank.

after I removed the script, it worked fine.

 

does anyone faced the same issue? or have the updated script?

Translate

Forum|alt.badge.img+3
  • 17 replies
  • April 28, 2019

And now the validation is not working properly in the lookup fields.

Translate

Forum|alt.badge.img+2
  • Novice
  • 8 replies
  • June 29, 2022

After i added that code then which is restricting more than 1 MB file after that i cannot upload any file from attachment controls? can you tell me any workaround?

 

 

 

Translate

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings