Trying to preview the attachment in lightning,but itsnot showing attachment properly.

Passing scenario:

For image file: It shows image fileS

Problem facing:


In case of PDF,itsnot loading properly

enter image description here

and How to provide download option for attachments?

Whats the issue in below code:


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

    <img src="{!v.imageSrc}" /> 

Javascript side:

   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);




 public class Controller {
   public static Attachment getImage(Id parentId){
 return [select Id, Name, ContentType from Attachment 
            where parentid=:parentId ] [0];
  • Are you facing issue only i community? – Pranay Jaiswal Dec 6 at 10:28
  • In the href link, try using "{!'/servlet/servlet.FileDownload?file='+ v.recordId}" – Paul Kissick Dec 7 at 18:04

You are doing a few things wrong. They are:-

  1. In your method getImage, Your return type is an attachment but you are returning a list. In case, the record having one attachment, your apex will get executed, but in case of more than one, It will get fail.
  2. When you are using aura: iteration, You need to pass an attribute of type list in items. You are using a value which is not even defined in your component markup.

Make sure you have used correct attribute for items in aura: iteration, then it will go inside the iteration.

  • Thanks for suggestion.i will make that change ....I have only one attachment for testing purpose..that's not displaying .. – Annappa PH Dec 9 at 4:30
  • cant we provide downoad option in the attchment – Annappa PH Dec 9 at 8:04
  • I have updated the question.Please check it – Annappa PH Dec 9 at 8:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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