[SalesForce] Get the image in file – lightning experience

I have to display an image in a lightning component. So in Salesforce classic I put this image in an attachment related to an object, but in lightning experience there is no attachment, or I didn't find them. There is only the possibility to add a file.

My question is how can I retrieve this file in my component ? For an attachment I retrieve the attachment like that :

List<Attachment> img = [select Id, Name, ContentType, parentId from Attachment 
                            where parentId in :listeId and ContentType in ('image/png', 'image/jpeg', 'image/gif')]; 

and in my component I put that :

<img src="{!'/servlet/servlet.FileDownload?file=' + a.image.Id}" alt=""/>

but it doens't retrieve me the url of the image in the file.

Best Answer

If you can get the base64 encoded string from the Attachment, you can use that as src of the image.

Example:

https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images#Embedding_an_image_via_data_URL

Also check this answer:

https://stackoverflow.com/a/9511375/6110447

Edit: Query for Body of the Attachment, it is base64 encoded.

Related Topic