Check attachment file type-client script

Sin
Giga Expert

Hi All,

I have written a client script to restrict attachment file types.I want to allow users to attach only .csv file types.

written code for both portal and non-portal, On portal its working as expected indexOf returns different value for all csv attachments.

On non-portal indexOf  returns -1 for all type of attachments. (jpg,pdf,xml.csv..)

 

function onSubmit() {
try
{
//for non-portal
var sysid =g_form.getElement('sysparm_item_guid').value;
var rowcount=0;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
while (attachment.next()) {
rowcount++;
}
if(rowcount != 1)
{
if(rowcount==0)
alert("Attachment Required!!");
return false;
}
else if(rowcount == 1 && attachment.content_type.indexOf(".csv") <= -1){
alert(attachment.content_type);
alert("Attachment should be in only csv format!");
return false;
}
else return true;

}


catch(e)

{
//for-portal
if(this.document.getElementsByClassName('get-attachment').length ==1) {
var value = (this.document.getElementsByClassName('get-attachment')[0].innerHTML).toLowerCase();
alert(value.indexOf('.csv'));
if ((value.indexOf('.csv')>-1)){
return true;
}
else
{
alert ("Attachment should be in only csv format!");
return false;
}
}
else if(this.document.getElementsByClassName('get-attachment').length <1)
{
alert( "Attachment Required!!");
return false;
}

}

 

Is there anything wrong in my code?Need some suggestion.

Thanks in Advance!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sana,

what content_type you are getting?

Did you alert that?

because content_type could be application/csv or application/vnd.ms-excel for a csv file

why not check file name itself i.e. filename contains .csv?

Also you can get the rowcount in client side and no need to use counter to increment in while loop

try{

//for non-portal
var sysid =g_form.getElement('sysparm_item_guid').value;

alert('sysid is  ' + sysid); // is this coming properly


var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
var rowCount = attachment.rows.length;
if(rowcount != 1)
{
if(rowcount==0)
alert("Attachment Required!!");
return false; 
}
else if(rowcount == 1 && attachment.content_type.indexOf(".csv") <= -1){
alert(attachment.content_type);
alert("Attachment should be in only csv format!");
return false; 
}
else return true;

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sana,

what content_type you are getting?

Did you alert that?

because content_type could be application/csv or application/vnd.ms-excel for a csv file

why not check file name itself i.e. filename contains .csv?

Also you can get the rowcount in client side and no need to use counter to increment in while loop

try{

//for non-portal
var sysid =g_form.getElement('sysparm_item_guid').value;

alert('sysid is  ' + sysid); // is this coming properly


var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
var rowCount = attachment.rows.length;
if(rowcount != 1)
{
if(rowcount==0)
alert("Attachment Required!!");
return false; 
}
else if(rowcount == 1 && attachment.content_type.indexOf(".csv") <= -1){
alert(attachment.content_type);
alert("Attachment should be in only csv format!");
return false; 
}
else return true;

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yea Ankur on non-portal am getting content type as ,

  • "application/vnd.ms-excel" for csv files
  • "image/png" for images
  • indexOf value as '-1' for all type of contents

 

Hi Sana,

So it means you can either compare the indexOf application/vnd.ms-excel or rely on the file name.

 

Is your file name containing csv then directly use file_name to check indexof csv instead of content_type

because content_type for excel and csv could be application/vnd.ms-excel 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Is there anyway to remove attached files once the alert comes ?

alert("Attachment should be in only csv format!");