3

How can I display an image which is stored in files?

For attachments I can use the code below, but it doen't work for files.

<apex:image id="Stat1" URL="/servlet/servlet.FileDownload?file=0698E0000008w5e" width="15" height="15"/>
1
  • I am afraid that this can be done, there is a similar question with no answer. For Attachment the URL can be hard-coded or can be retrieved from $Action variable. As for Files, its not possible to preview it, but maybe hardcode URL to download it.
    – Raul
    Aug 7, 2017 at 8:59

5 Answers 5

5

Try this format <apex:image url="/sfc/servlet.shepherd/version/download/[DOWNLOADID]"/>.

How to get the DOWNLOADID?

Open the File, you will get a screen as below. Hover on the download link, you will get the DOWNLOADID as highlighted. Replace [DOWNLOADID] with the ID of the ContentVersion record you want to display.

Example:

<apex:image url="/sfc/servlet.shepherd/version/download/068i0000003ebfw"/>

enter image description here

VF Page :

<apex:page standardController="Account" >
    <apex:image url="/sfc/servlet.shepherd/version/download/0680H0000036GIH"/>
</apex:page>

Screenshot:

enter image description here

2
  • This would allow an user to download file instead of a preview.
    – Raul
    Aug 7, 2017 at 9:39
  • @Rahul My bad. Please find the updated answer, I somehow didn't notice that the ContentVersion ID is not exactly same as the DownloadID as explained in my answer. Thanks for the observation :)
    – Saroj Bera
    Aug 7, 2017 at 11:29
1

Working Code, Thanks to the answer above.

My VF with a Repeat Across a list of Files:

<apex:repeat value="{!files}" var="f">
    <div class="col col-6 col-md-4 col-lg-3 col-xl-2 my_vertmargin5">
        <div class="my_padded5 my_panel">
            <apex:image url="/sfc/servlet.shepherd/version/download/{!f.ContentDocument.LatestPublishedVersionId}"
                            alt="{!f.ContentDocument.LatestPublishedVersion.Title}" height="150px"/>
        </div>
    </div>
</apex:repeat>

My Controller Method with respect to a single record that the files are linked to:

public void GetFiles(){
    files = [SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId,
    ContentDocument.LatestPublishedVersion.Title
    FROM ContentDocumentLink WHERE LinkedEntityId = :Job.Id];
}

NB: other code required like instantiating variables etc.

0

I've tried the solutions above with no success after few tries managed to view the image stored on files in my lightning component with similar url:

/sfc/servlet.shepherd/document/download/<YOUR FILE ID>
1
  • This is incorrect: it should be VERSION: <apex:image url="/sfc/servlet.shepherd/version/download/{!f.ContentDocument.LatestPublishedVersionId}" alt="{!f.ContentDocument.LatestPublishedVersion.Title}" height="150px"/> Feb 11, 2020 at 9:36
0

Thanks @all,

I've compiled all the info I got here for others: ​​​​​​​https://www.mhamzas.com/blog/2020/01/09/use-salesforce-files-image-file-on-lwc-aura-or-vf-page-as-tag/

1
  • This is also incorrect, Again use "version". The higher ranked answer is correct Feb 11, 2020 at 9:37
0

Here's a complete solution, thanks to NZ Dev above, to display all Files attached to a record:

Apex Class:

public class FileViewerExtension {
    public ID recordId {get;set;}
    public list <ContentDocumentLink> files {set;get;}
    
    public FileViewerExtension(ApexPages.StandardController controller) {
        recordId = System.currentPageReference().getParameters().get('id');
        files = [SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, ContentDocument.LatestPublishedVersion.Title FROM ContentDocumentLink WHERE LinkedEntityId = :recordId];
    }
}

VFpage:

<apex:page standardController="YOUROBJECT__c" extensions="FileViewerExtension">
    <apex:repeat value="{!files}" var="f">
        <apex:image url="/sfc/servlet.shepherd/version/download/{!f.ContentDocument.LatestPublishedVersionId}" width="150px"/>
    </apex:repeat>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .