0

Apex :

private static final String  BASE_URL = URL.getSalesforceBaseUrl().toExternalForm();
@AuraEnabled
public static List<FileData> telechargerConv(String recordId){
  List<ContentDocumentLink> links=[SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=:recordId];
    Set<Id> ids=new Set<Id>();
    for(ContentDocumentLink link:links)
    {
        ids.add(link.ContentDocumentId);
    }
    List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
    
    List<FileData> files=new List<FileData>();
    for(ContentVersion attach:versions)
     {
        FileData data=new FileData();
        if(versions!=null && versions.size()>0)
        {
            data.Content = EncodingUtil.base64Encode(versions[0].VersionData);
            data.ContentType = ContentType(versions[0].FileExtension);
        }
        data.DownloadUrl =BASE_URL + '/sfc/servlet.shepherd/document/download/'+versions[0].ContentDocumentId;
        data.FileUrl =BASE_URL + '/sfc/servlet.shepherd/version/renditionDownload?rendition=THUMB720BY480&versionId='+versions[0].Id;
        files.add(data);
    }
    return files;
}
@AuraEnabled
public static string ContentType(string fileType)
{
    switch on fileType.toLowerCase()
    {
        when 'docx'
        {
            return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        }
        when 'csv'
        {
            return 'application/vnd.ms-excel';
        }
        when 'wav'
        {
            return 'audio/wav';
        }
        when 'wmv'
        {
            return 'video/x-ms-wmv';
        }
        when 'mp3'
        {
            return 'audio/mpeg';
        }
        when 'mp4'
        {
            return 'video/mp4';
        }
        when 'png'
        {
            return 'image/png';
            
        }
        when 'pdf'
        {
            return 'application/pdf';
            
        }
        when else {
            return 'image/jpeg';
        }
    }
}
public class FileData 
{
    @AuraEnabled
    public string Content{get;set;}
    @AuraEnabled
    public string ContentType{get;set;}
    @AuraEnabled
    public string FileUrl{get;set;}
    @AuraEnabled
    public string DownloadUrl{get;set;}
}

JS :

telechargerConv({ recordId : currentRow }).then(result => {

        console.log('download',result);
    })

i retrieve well my files :

enter image description here

But i'm stuck on how to run the Url and download the files ..

The URL should be like this in order to download file in community ?????:

https://DOMAIN.sandbox.lightning.force.com/sfc/servlet.shepherd/document/download/FILEID

1 Answer 1

1

You have run a loop on your result and navigate to download url using window.open like this:-

telechargerConv({ recordId : currentRow }).then(result => {
    result.forEach(element => {
        window.open(element.DownloadUrl, '_blank');
    });
})

This will open a new tab, download the file and close it automatically for each of the item in your result.


Update

the download url should look like this:-

https://db0000000dzqemau-dev-ed.develop.lightning.force.com/sfc/servlet.shepherd/version/download/068B000000BU4wNIAT?operationContext=S1

i.e window.location.origin +'/sfc/servlet.shepherd/version/download/'+ version id+'?operationContext=S1'

you need to make sure you have download url as above format

Again Update

if you are in community, then your url should be like this:-

String fileDownloadUrl = URL.getSalesforceBaseUrl().toExternalForm()  + '/sfc/servlet.shepherd/document/download/' + contentDocument Id + '?operationContext=S1';

the only difference i can see in your code is you are missing '?operationContext=S1' in url

Another way of accessing the download url is using contentdistribution object

sample example can be found here:- https://gist.github.com/lukethacoder/818d4dba3f7fb00ea861a76fe85fc1a3

15
  • the issue is related to DownloadUrl , even when i tried to execute the url in new window it doesn't work .. file doesn't open in community
    – user122708
    Commented Oct 10, 2022 at 14:27
  • @user122708, please check the update Commented Oct 10, 2022 at 14:33
  • my download URL looks like this : https://"DOMAIN".sandbox.my.site.com/sfc/servlet.shepherd/document/download/"FileID" , the button is located in Community portal page
    – user122708
    Commented Oct 10, 2022 at 14:38
  • @user122708 I have added the update again, please check Commented Oct 10, 2022 at 14:50
  • So the right url is this : data.DownloadUrl =URL.getSalesforceBaseUrl().toExternalForm() + '/sfc/servlet.shepherd/document/download/' + contentDocument.Id + '?operationContext=S1'; in order to download all files ? on click buton can't understand why it redirect me to another url ( login to another community ) : https://DOMAIN.sandbox.my.site.com/"IDEES"/login?ec=302&in...
    – user122708
    Commented Oct 10, 2022 at 14:59

You must log in to answer this question.