Activity Forums Salesforce® Discussions How to display an image stored in Document in a Salesforce Visualforce page?

  • Prakhar

    Member
    September 8, 2016 at 12:09 pm

    Hi Tanu,

    Please read the below code to display the image from document on to a VF page.

    ApexClass:-

    public with sharing class ImageController {
    public String imageURL{get;set;}

    public ImageController()
    {
    imageURL='/servlet/servlet.FileDownload?file=';
    List< document > documentList=[select name from document where
    Name='SamplePic'];

    if(documentList.size()>0)
    {
    imageURL=imageURL+documentList[0].id;
    }
    }
    }

    VF Page:-

    <apex:page controller="ImageController" showheader="false" sidebar="false">

    <apex:form>
    <apex:image url="{!imageURL}">
    </apex:image></apex:form>

    </apex:page>

    Thanks.

  • Senthil

    Member
    June 14, 2017 at 11:36 pm

    You can easily avoid having a controller by just using custom label.

    1. Create the document and note down the document Id. Mark the document externally available if needed
    2. Note down your org id (Setup > Company Information > Salesforce.com Organization ID)
    3. Crate a custom Label with the value in this format: <domain>/servlet/servlet.ImageServer?id=[docid]&oid=[OrgId]
    4. Example: cs71.salesforce.com/servlet/servlet.ImageServer?id=[docid]&oid=[OrgId]
    5. Note down the the custom label name
    6. In the visualforce page, add the below snippet
    7. <apex:form >
      <apex:image url="{!$Label.Image_URL}">
      </apex:image>
      </apex:form>
    8. <Image_URL> is the name of the custom label defined in step 3.
    9. If you don't want to use custom label, you can directly hard code the url as well in the VF page.

    Hope this helps

    • This reply was modified 7 years, 5 months ago by  Senthil. Reason: modifed #3

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos