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

  • How to display an image stored in Document in a Salesforce Visualforce page?

    Posted by Tanu on September 7, 2016 at 2:10 PM

    Hii All,

    I’m trying to display an image that is stored as a Document. Starting with the most basic example, I am using the following:

    <apex:page standardcontroller=”Document”>

    <apex:outputField value=”{!Document.name}”/>
    <apex:image id=”theImage” value=”!Document.Logo.jpg” width=”100″ height=”100″/>

    </apex:page>

    Senthil replied 9 years ago 3 Members · 2 Replies
  • 2 Replies
  • 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.

  • [adinserter block='9']
  • 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 9 years ago by  Senthil. Reason: modifed #3

Log In to reply.