Newer Version Available
Customize File Downloads
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 download is allowed, and what to do otherwise. The ContentDownloadContext enum is the context in which the download takes place.
You can use Apex to customize multiple-file downloads from the Content tab in Salesforce Classic. The Apex function parameter List<ID> handles a list of ContentVersion IDs.
Customization also works on content packs and content deliveries. List<ID> is a list of the version IDs in a ContentPack. Setting isDownloadAllowed = false on a multi-file or ContentPack download causes the entire download to fail. You can pass a list of the problem files back to an error page via URL parameters in redirectUrl.
Example
- Prevent a file from downloading based on the user profile, 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.ContentDownloadHandlerFactory are 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.