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

Hi Ankur, I am trying to do same but allowing user to enter .txt file only. Could you please check my code why its not working? it is accepting all the files.

var sysid =g_form.getElement('sysparm_item_guid').value;
alert(sysid );
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;
var file = attachment.file_name; // this is not working
alert(rowcount);
alert(file); // alert is not working

alert(attachment.content_type) // this is coming as undefined

if(rowcount==0 )
{
alert("Attachment Required!!");
return false;
}
else if(rowcount == 1 && attachment.content_type.indexOf(".txt") >= -1){

alert("Attachment should be in only txt format!");
return false;
}

@sraj 

It is not recommended to use GlideRecord in client script

Regards
Ankur

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

Hi ankur needs to add only csv file but....... attachment.content_type.indexOf(".csv") <= -1) this is returning -1 or csv,txt file also ..please help me with that

Can you please post a new question for this as this thread is already answered.

Please tag me in that question and share all the details

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

Ahmmed Ali
Mega Sage

Issue is in code after while loop.

 

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==0){ // Missing {}
alert("Attachment Required!!");
return false; 
}
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;
}

}
}
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali