This question already has an answer here:

I have attachments (images) for an object and here's how I am querying for my data from the object.

public List<SObject> getResults () {
    Id id = ApexPages.currentPage().getParameters().get('id');
    List<SObject> results = [SELECT Id, 
                                    (SELECT Id, Name FROM Attachments)
                                    FROM MyCustomObject__c 
                                    Id = :id];
    return results;

This is how I am rendering this data in visualforce

<apex:repeat var="c" value="{! results }">
                <div class="typeHeader">
                    {! c.Name}

                <apex:outputText rendered="{! c.URL__c != null }">
                    <div class="vulnDescrip">
                        {! c.URL__c}

How can I render the attachments similar to how I am rendering URL__c and Name?

marked as duplicate by Tushar Sharma, Reshma, David Reed apex Jan 23 at 12:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You may try this.

<apex:repeat value="{!Case.attachments}" var="att">

                <apex:panelgrid columns="1">
                    <apex:image url="{!URLFOR($Action.Attachment.Download, att.id)}"/>

You cannot query related attachments body in one soql.

Query child attachments in separate soql.

Attachment att = [Select Id, Body, Name FROM Attachment WHERE ParentId = :results[0].Id];

You can have a public variable to encode its value as

public String image {get;set;}
image = EncodingUtil.Base64Encode(att.body);

Refer this image in vf page as:

<img src="data:image/png;base64,{!image}" />
  • We don't need the body here. This can be achieved without query body. – Tushar Sharma Jan 23 at 6:57
  • How can we achieve this without query body? – Illusion Jan 23 at 7:05
  • @Illusion SFDC champ's answer will work. – Tushar Sharma Jan 23 at 7:08
  • Yes. The other answer will work. – Reshma Jan 23 at 7:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.