Loading

Display Base64 data on a Visualforce page

Publish Date: Oct 13, 2022
Description

Sample code snippet to display Base64 Data on a page layout using Visualforce Page.
 

PageReference Methods 

Note: Also notes and attachments added in classic , are being considered as attachments and for notes and attachments uploaded in lightning experience are considered as ContentDocument.

 

 

 

Resolution
<apex:page controller="ViewImage" cache="true">
      <img src="data:{!att.ContentType};base64,{!image}" /> </apex:page>

public class ViewImage {
      public Attachment att {
            get {
                  if (att == null) {
                        String id = ApexPages.currentPage().getParameters().get('AttachmentID');
                        att = [SELECT Body, ContentType, Name FROM Attachment WHERE ID = :id];
                  }
                  return att;
            }
            private set;
      }
      public String image {
            get {
                  return EncodingUtil.Base64Encode(att.body);
            }
      }
}
Knowledge Article Number

000385186

 
Loading
Salesforce Help | Article