1

I need to display th elist of the attachment of a case in a lightning component.

Here is the lightning component :

 <aura:attribute name="recordId" type="Id" required="true"/>
<aura:attribute name="files" type="List"/>

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<div class="LC11Container" aura:id="listFiles">

    <c:fileUpload parentId="{!v.recordId}"/>

    <aura:iteration items="{!v.files}" var="currentFile">

        <h3 class="slds-truncate" title="{!currentFile.Name}">
            <a href="{!'/servlet/servlet.FileDownload?file='+currentFile.Id}">{!currentFile.Name}</a>
        </h3>
</aura:iteration>
</div>

The javascript controller of the component :

doInit : function(component, event, helper) {
    var vAction = component.get("c.getAllFiles");
    console.log('############# component.get("v.recordId") : ' + component.get('v.recordId'));
    vAction.setParams(
    {
        pCaseId : component.get('v.recordId')
    });
    vAction.setCallback(this, function(pResponse) {
        var vState = pResponse.getState();
        if (vState === "SUCCESS") { 
            component.set('v.files', pResponse.getReturnValue());
            console.log(pResponse.getReturnValue());
        } else {
            var vErrors = pResponse.getError();
            if (vErrors) {
                if (vErrors[0] && vErrors[0].message) {
                    console.log("Error message: " + vErrors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }
    });
    $A.enqueueAction(vAction);
},

And the apex controller, that query the attachments and return a wrapper class :

public class Doc {
    @AuraEnabled
    public String name;
    @AuraEnabled
    public DateTime fileDate;
    @AuraEnabled
    public String fileId;
    @AuraEnabled
    public String fileType;
} 

@AuraEnabled
public static List<Doc> getAllFiles(String pCaseId) {
    List<Doc> vDocs = new List<Doc>();

    List<Attachment> lAtt = [Select Id, Name, ContentType, LastModifiedDate, BodyLength
                            from Attachment 
                            where ParentID=:pCaseId];


    for(Attachment att : lAtt){
        Doc vDoc = new Doc();
        vDoc.fileId = att.Id;
        vDoc.name = att.Name;
        vDoc.fileType = att.ContentType;
        vDoc.fileDate = att.LastModifiedDate;

        vDocs.add(vDoc);
    }

    System.debug('### vDocs : ' + vDocs);

    return vDocs;
}

So this display me a list of attachments (the names) but when I click it it displays the document but did not downoload it or allow me to chose to download it on close it. It juste display the document and I can't even close it without return to last page with the browser.

Can I display link to download the document ?

  • why cant you try "lightning:fileUpload"??? – Annappa PH May 23 '18 at 10:09
  • Because I have to work with attachments and not files ... I know that there is better to work with files but for now I havn't the choice unfortunately – dou May 23 '18 at 11:55
1

Make sure you've set the download="{!attachment.Name}" on the link. Without it, the browser will treat it as a page, and not a file.

| improve this answer | |
0

<aura:attribute name="recordId" type="Id" required="true"/> <aura:attribute name="files" type="List"/>

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<c:fileUpload parentId="{!v.recordId}"/>

<aura:iteration items="{!v.files}" var="currentFile">
    

        <h3 class="slds-truncate" title="{!currentFile.name}">
            <a href="{!'/servlet/servlet.FileDownload?file='+currentFile.fileId}">{!currentFile.name}</a>
        </h3>
    </aura:iteration>
| improve this answer | |

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.