I want to display an image, which is in an attachment in a customized object, in lightning component. I make a query like that in the server side controller :

public static Attachment getImage(Id parentId){
        throw new System.NoAccessException();
        return null;

    return [select Id, Name, ContentType from Attachment 
            where parentid=:parentId and ContentType in ('image/png', 'image/jpeg', 'image/gif')];

Here is the component :

<aura:component controller="Controller"  implements="forceCommunity:availableForAllPageTypes,force:appHostable,flexipage:availableForAllPageTypes">
<aura:attribute name="oct" type="sObject__c[]"/>
<aura:attribute name="actId" type="Id"/>
<aura:attribute name="imageSrc" type="String" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<aura:iteration var="a" items="{!v.act}">
    <div>{!a.Name} : {!a.Description__c}</div>
    <img src="{!v.imageSrc}" /> 

and the part of the client side controller :

doInit : function(component, event, helper) {

    var actionImage = component.get("c.getImage");
        parentId : component.get("v.actId")

    actionImage.setCallback(this, function(rep){
        var attachment = rep.getReturnValue();
        component.set("v.imageSrc", "/servlet/servlet.FileDownload?file=" + attachment.Id);



So I got an error for this line, I guess : component.set("v.imageSrc", "/servlet/servlet.FileDownload?file=" + "attachment.Id"); because of the Id. And if I put a "true" Id I see the image, but I don't see how I can retrieve the good Id dynamically...

1 Answer 1


You can remove double quotes as attachment is JavaScript variable and the correct syntax is:

component.set("v.imageSrc", "/servlet/servlet.FileDownload?file=" + attachment.Id);
  • Yes sorry I forgot to correct it in my question, but even without the quotes it doesn't work...
    – dou
    Mar 9, 2016 at 16:22
  • Console.log the return object ..that will help you log response variable Mar 9, 2016 at 16:23
  • if i put console.log(attachment) it says that it's null... How can I set a value in it ?
    – dou
    Mar 9, 2016 at 16:25
  • This means your server is returning null.put system.debug and debug apex code.. Mar 9, 2016 at 16:26
  • 1
    Shouldn't it be "...servlet.FileDownload...", instead of "...servlet.File download..."?
    – Gusk
    Aug 31, 2016 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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