Download multiple Files & Documents as a ZIP file in Salesforce

If your requirement is to download multiple files in a zip in a Lightning component or Lightning Web Component or Visualforce page, then all you need to do is construct a URL which looks like this
‘/sfc/servlet.shepherd/version/download/068XXXXXXXXXXX1/068XXXXXXXXXXX2/068XXXXXXXXXXX3?’

here 068XXXXXXXXXXX1 is ContentVersion Id of file 1, 068XXXXXXXXXXX2 of file 2 and you can keep on adding as many file Ids as supported by URL length max URL length is (2,048) which means you can get 100~110 files like this at once depending upon the domain length of your org.

you can download a large file, by large I mean file with size 500mb+ easily.

Let’s create a Lightning Web Component which will download all the files in a ZIP file

Create an Apex Class RecordFileDownloaderController

public with sharing class RecordFileDownloaderController {

    @AuraEnabled
    public static List<Id> getfileIds(String recordId){
        Set<Id> contentDocumentIds = new Set<Id>();
        List<Id> contentVersionIds = new List<Id>();
        
        if(String.isNotBlank(recordId)) {
            for(ContentDocumentLink cdl : [SELECT ContentDocumentId,LinkedEntityId,ContentDocument.FileType FROM ContentDocumentLink WHERE LinkedEntityId =:recordId]) {
                contentDocumentIds.add(cdl.ContentDocumentId);
            }
        }

        if(contentDocumentIds.size() > 0) {
            for(ContentVersion cdv : [SELECT Id FROM ContentVersion WHERE isLatest = true AND ContentDocumentId IN :contentDocumentIds]) {
                contentVersionIds.add(cdv.Id);
            }
        }

        return contentVersionIds;
    }
}

Now Create a new Lightning Web Component recordFileDownloader

<template>
    <template if:true={error}>{error}</template>
    <a href={getDownloaLink} variant="brand" label="" title="Download Files" class="slds-button slds-button_brand">Download Files</a>
</template>
import { LightningElement, track, api} from 'lwc';
import getfileIds from '@salesforce/apex/RecordFileDownloaderController.getfileIds';

export default class RecordFileDownloader extends LightningElement {
    @api recordId;
    @track fileIds = '';
    @track error = '';
    
    connectedCallback() {

        getfileIds({
            recordId : this.recordId
        })
        .then(result => {
            let fileDataList = JSON.parse(JSON.stringify(result));
            let fileIdsString = '';
            if(fileDataList) {
                for (let i in fileDataList) {
                    fileIdsString += fileDataList[i]+'/';
                }
            }
            if(fileIdsString.length > 0) {
                fileIdsString =fileIdsString.replace(/.$/,"?");
            }
            this.fileIds = fileIdsString;
            this.error = undefined;
        })
        .catch(error => {  console.log('error : '+JSON.stringify(error));
            this.error = error;
        });
    }
    
    get getDownloaLink() {
        return '/sfc/servlet.shepherd/version/download/'+this.fileIds;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="recordFileDownloader">
    <apiVersion>47.0</apiVersion>
    <masterLabel>Mass File Downloader</masterLabel>
    <isExposed>true</isExposed>
    <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>
Mass file download LWC Component

refer this Git Repo for Code

Was this post useful? If yes then please share.



Leave a Comment

All fields marked with an asterisk (*) are required


shares