0

I want to restrict a file upload based on extension type and mime type. However i found the fileextension field will always be null in before or after insert contentversion trigger and filtype field on content will have some different form of string. Ex- For excl filetype is "EXCEL_X" and file extension is null in after insert trigger. I want to get the mime type like 'applicaton/xlxs' or somewhat like that.

  • @DavidReed I checked that answer earlier, The mime type can be multiple for excel file only. Let me know how can we identify the mime type by just seeing Filetype – Sukruti Sep 3 '18 at 16:41
  • FileType isn't a MIME type. It's a Salesforce-specific string. If that's what you're after, it'd be helpful if you could clarify that in your question, because the actual MIME type (like 'text/csv') isn't available. – David Reed Sep 3 '18 at 16:52
  • @DavidReed Is there any way to get Salesforce specific string for different type of file ? Any documention if you know.. Also, Thanks i edited my question. – Sukruti Sep 3 '18 at 17:00
  • not that I know of, besides uploading files of various types and looking at the field after Salesforce populates it. – David Reed Sep 3 '18 at 17:03
0

When you talk about checking the file type in before insert, Better to go with PathOnClient field on ContentVersion.It is not null in both before and in after trigger.

trigger ContentVersiontrigger on ContentVersion (before insert) {
    for(ContentVersion cv:Trigger.new){
        if(cv.PathOnClient==cv.Title+'.jpg'){
          cv.adderror('error');
        }
    }}

PathOnClient will contain the Title and file type separated by "." e.g PathOnClient=download.png

If you are going for 'after trigger' on ContentVersion, then Yes you will get FileType 'EXCEL_X' whose extension is XLSX.

Again, Here pathOnclient will contain the file extension which will take out you from all the problem.

  • Hi Sanket, Is it possible to get mime type from any file ? like 'application/xlsx' ..Like the way we have content-type field on attachment – Sukruti Sep 3 '18 at 17:01
  • 1
    ContentVersion does not contain mime type field as I could check in the documentation. – sanket kumar Sep 3 '18 at 17:28
0

You can try with the field PathOnClient

Description - The complete path of the document. One of the fields that determines the FileType.

code snippet inside your trigger/handler should be -

     ContentVersion attachedFile = new ContentVersion();

   if (attachedFile.PathOnClient.endsWithIgnoreCase('xlsx') || attachedFile.PathOnClient.endsWithIgnoreCase('xls')){
    attachedFile.addError('xls and xlsx files are allowed');
    }
  • Hi Bennie, Is it possible to get mime type from any file ? like 'application/xlsx' ..Like the way we have content-type field on attachment – Sukruti Sep 3 '18 at 17:04
  • The field ContentType will contain the MIMEType of a file in the Attachment object. Not sure about ContentVersion – Jarvis Sep 3 '18 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.