Salesforce Apex Customize File Download Behavior

You can customize the behavior of files when users attempt to download them using an Apex callback. ContentVersion supports modified file behavior, such as antivirus scanning and information rights management (IRM), after the download operation. File download customization is available in API version 39.0 and later. Customization code runs before download and determines whether the download can proceed.The Sfc namespace contains Apex objects for customizing the behavior of Salesforce Files before they are downloaded.   ContentDownloadHandlerFactory provides an interface for customizing file downloads. The ContentDownloadHandler class defines values related to whether the download is allowed, and what to do otherwise.

Use cases 

  • Prevent a file from downloading based on the user profile, the device being used, or file type and size.
  • Apply IRM control to track information, such as the number of times a file has been downloaded.
  • Flag suspicious files before download, and redirect them for antivirus scanning.

Flow Execution

When a download is triggered either from the UI, Connect API, or an sObject call retrieving ContentVersion.VersionData, implementations of the Sfc.ContentDownloadHandlerFactoryare looked up. If no implementation is found, download proceeds. Otherwise, the user is redirected to what has been defined in the ContentDownloadHandler#redirectUrl property. If several implementations are found, they are cascade handled (ordered by name) and the first one for which the download isn’t allowed is considered.

Custom File Download Examples

 You can use Apex to customize the behavior of files upon attempted download. These examples assume that only one file is being downloaded. If the user with 005xx000001SvogAAC id is not trying to download any file from then it will redirect to the visualforce page.

Apex Class

public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
    public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
        Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
        if(UserInfo.getProfileId() == '005xx000001SvogAAC') {
            contentDownloadHandler.isDownloadAllowed = true;
            return contentDownloadHandler;
        if(context == Sfc.ContentDownloadContext.MOBILE) {
            contentDownloadHandler.isDownloadAllowed = false;
            contentDownloadHandler.downloadErrorMessage = 'Downloading a file from a mobile device isnt allowed.';
            return contentDownloadHandler;
        contentDownloadHandler.isDownloadAllowed = false;
        contentDownloadHandler.downloadErrorMessage = 'This file needs to be IRM controlled. Youre not allowed to download it';
        contentDownloadHandler.redirectUrl ='/apex/IRMControl?Id='+ids.get(0);
        return contentDownloadHandler;


public class IRMController {
    public ContentVersion versionDetails{get;set;}
    public IRMController() {
        String versionId = ApexPages.currentPage().getParameters().get('id');
        versionDetails = [Select Id ,VersionNumber,Description ,SharingOption,SharingPrivacy,Title from ContentVersion where id=:versionId] ;



<apex:page controller="IRMController" lightningStylesheets="true">
    <apex:slds />
    File Cannot be download . Please find the details here .<br/>
    VersionNumber :  {!versionDetails.VersionNumber}<br/>
    Description :     {!versionDetails.Description}<br/>
    SharingOption :   {!versionDetails.SharingOption}<br/>
    SharingPrivacy:    {!versionDetails.SharingPrivacy}<br/>
    Title:   {!versionDetails.Title}

Now if the user is trying to download the file it will redirect to the page and not able to download the file.