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

 

Page 

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