function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cbernalcbernal 

Validate attachment's extension

Hi,

 

Anyone knows how to check the extension file for an attachment.

 

I have this Trigger:

 

trigger checkAttachment on Attachment (before insert) {
    For(Attachment att:Trigger.new)
    {
        if(att.ContentType=='.exe')    
        {
            att.addError('Cannot upload file with extension .exe');                    
        }
    }    
}

 

But it don't work, it still accepts files with .exe extension.

 

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

The filed ContentType will contain the MIMEType of a file. So .EXE will be 'application/octet-stream', you can find a list of different MIMETypes online. Here's the first one I found on google. http://www.webmaster-toolkit.com/mime-types.shtml

 

You can also accomplish this by just checking your name of the file you uploaded IE: att.Name.endsWith('.exe')

All Answers

Alex.AcostaAlex.Acosta

The filed ContentType will contain the MIMEType of a file. So .EXE will be 'application/octet-stream', you can find a list of different MIMETypes online. Here's the first one I found on google. http://www.webmaster-toolkit.com/mime-types.shtml

 

You can also accomplish this by just checking your name of the file you uploaded IE: att.Name.endsWith('.exe')

This was selected as the best answer
cbernalcbernal

Thanks

 

I solved it with 

 

 

if(att.Name.endsWith('.exe'))    

but I still have to validate the .zip or .rar files with a .exe file.

Do you know how can I do it?

 

Alex.AcostaAlex.Acosta

I believe APEX is currently unable to read/extract ZIP / RAR files natively.