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
Jonathan Wolff 7Jonathan Wolff 7 

How to block the upload of specific document types in salesforce?

Hi, I would like to block specific document types to be uploaded in salesforce. Is there a possibility to apply a filter so there i.e could only be uploaded jpeg's?
Greetings
Jonathan
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jonathan,

>> https://salesforce.stackexchange.com/questions/121123/how-to-restrict-file-type-in-standard-page-attach

In the above link there is a implementation that you can use I am adding it below for quick reference:
 
Here is how I able to solve the above issue:

public static final List<string> CONTENT_TYPES = new string[]{'image/gif','image/png','image/jpg'};

for(attachment a: Trigger.new)
{
   if (!isAllowedContentType(a.ContentType)) {
       a.addError('only allowed jpg, gif, png files');
   }    
}
Private helper method:

   private static boolean isAllowedContentType(string selCT)
   {
      for(String ct : CONTENT_TYPES) 
      {
          if(ct == selCT) {
            return true;
          }
      } 
      return false; 
   }

If you are building custom lightning component implementation then as mentioned in the documentation https://developer.salesforce.com/docs/component-library/bundle/lightning-file-upload/documentation you can make use of acceptedformats using accept attribute to the tag as implemented in the below snippet:
 
<template>
    <lightning-file-upload
            label="Attach receipt"
            name="fileUploader"
            accept={acceptedFormats}
            record-id={myRecordId}
            onuploadfinished={handleUploadFinished}
            multiple>
    </lightning-file-upload>
</template>

import { LightningElement, api } from 'lwc';
export default class FileUploadExample extends LightningElement {
    @api
    myRecordId;

    get acceptedFormats() {
        return ['.pdf', '.png'];
    }

    handleUploadFinished(event) {
        // Get the list of uploaded files
        const uploadedFiles = event.detail.files;
        alert("No. of files uploaded : " + uploadedFiles.length);
    }
}

>> https://help.salesforce.com/articleView?id=000319977&type=1&mode=1

As mentioned in the above if you want to restrict the upload of specific type to chatter you can use the trigger and add to the condition the necessary file types that aren't allowed.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Jonathan Wolff 7Jonathan Wolff 7
Hello, thank you for the help could you tell me where to add the code so I can try it in my Sandbox? Do I have to make it in a Aura Component?