I need to check the file attachment size on the form on submit

Barnali Rakshit
Tera Contributor

Need to write a Client Script where the attachment file should not be bigger than 5 MB , after uploading the file , when click on Submit it should throw an alert message and user should not be able to submit the request before uploading the right size file.

 

 

4 REPLIES 4

Musab Rasheed
Tera Sage
Tera Sage

Hello,

Check this thread, It has code

https://community.servicenow.com/community?id=community_question&sys_id=c71a43661bdd30507a5933f2cd4bcb40

Please hit like and mark my response as correct if that helps

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

Barnali Rakshit
Tera Contributor

Hi 

I have tried the below code but it is not working

function onSubmit() {
   //Type appropriate comment here, and begin script below
    var sys_id = g_form.getUniqueValue();
    var gr = new GlideRecord('sys_attachment');
    
    //gr.addQuery("table_sys_id", cat_id);
    gr.addQuery('table_sys_id',sys_id );
    gr.Query();
    
    var totalSize=0;
    while(gr.next()) {
    
        
        totalSize = parseInt(gr.size_bytes);
        totalSize=(totalSize / Math.pow(1024, 2)).toFixed(2) * 1;
    
    
    if(totalSize > 5) {
    alert("Attachment size exceeded");
        
        return false;
    }
    }
    
}

 

Musab Rasheed
Tera Sage
Tera Sage

Hello,

If you are fine with my response, Please hit like and mark my response as correct if that helps.

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

Pritam Mangrul1
Tera Expert

Hi, Add the below variable attribute for the attachment variable:

max_file_size=5

 

here 5 denotes 5 MB, you can change number as per your requirement.

 

Thanks,

Pritam