function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luciano RobertoLuciano Roberto 

download files

Hello Folks,

I created a list that loads all attachments related to a custom object, but I need to download them and I am trying to create a link unsuccessfully, someone could help me.
Follow my code

 
AttachmentList.cmp
-------------------------------------------------------------------------------------------------------------




<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="AttachmentListController"> 
    
     
      
     <aura:attribute name="recordId" type="Id"/>
     <aura:attribute name="files" type="List"/>
     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    
    
    
    <table class="slds-table slds-table_cell-buffer slds-table_bordered">
             <thead>
            <tr class="slds-line-height_reset">
              <th scope="col"><div class="slds-truncate" title="Name">Anexos</div></th>
              <th scope="col"><div class="slds-truncate" title="tipo">Tipo</div></th>

            </tr>
          </thead>
          
        
        <tbody>
           <aura:iteration items="{!v.files}" var="obj">
                <tr>
                    <th scope="row"> <a href="{!'/portal/servlet.shepherd/version/download/'+obj.Id}">{!obj.Title}</a>   </th> 

                    <!-- <th scope="row"> <a href="{!'/portal/servlet/servlet.FileDownload?file='+obj.Id}" >{!obj.Title}</a>   </th> -->
                  
                  <th scope="row"><div class="slds-truncate" title="Type">{!obj.FileType}</div></th>
                    
                   
                   
                    <td>
                    
                        
	        </td>
                </tr>
            </aura:iteration>
          </tbody>
        </table>
    
    
    
    
	
</aura:component>

AttachmentListController.js
------------------------------------------------------------------------------------------------------------



({
	
    

    doInit : function(component, event, helper) {
			  $A.enqueueAction(component.get('c.getList'));
	},
    
    
    doInit : function(component, event, helper) { 
        $A.enqueueAction(component.get('c.getList')); 
    }, 
    
    
    getList: function(component, event, helper) { 
        var action = component.get("c.getContentDocs"); 
        action.setParams( { 
            arecordId : component.get('v.recordId')
        }); 
        action.setCallback(this, function(actionResult) {
            component.set('v.files',actionResult.getReturnValue()); 
        }); 
        $A.enqueueAction(action); 
    },
    
    
    
    
    
    

    
    
})

AttachmentListController.apxc
---------------------------------------------------------------------------------------------------------



public class AttachmentListController {
    
  @AuraEnabled
    public static List<ContentDocument> getContentDocs(Id arecordId) {
        List<ContentDocumentLink> CDLs = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :arecordId];
        if (CDLs.size() < 1) {
            return new List<ContentDocument>();
        }
        List<Id> CDIdList = new List<Id> (); 
        for (ContentDocumentLink nextCDL : CDLs) { 
            CDIdList.add(nextCDL.ContentDocumentId); 
        } 
        List<ContentDocument> entries = [SELECT Id, Title, FileType FROM ContentDocument WHERE Id IN :CDIdList]; 
        return entries;
    }
     
    
    

}

 
Best Answer chosen by Luciano Roberto
Ankit RathorAnkit Rathor
Hi Luciano,

I did walkthrough your code and found that the url which you are using for file download is not correct.

You can download file (Content Document) in lightning directly by using file id like that 
<a href = "BASE_URL/sfc/servlet.shepherd/document/download/File_ID" >Download</a>

Use This like 
<a href="{!'/sfc/servlet.shepherd/document/download/'+ v.Id}" target="_blank">DOWNLOAD</a>

Please try this code:
<aura:iteration items="{!v.files}" var="obj">
    <tr>
        <th scope="row"><a href="{!'/sfc/servlet.shepherd/document/download/'+obj.Id}" target="_blank">{!obj.Title}</a></th>
        <th scope="row">
            <div class="slds-truncate" title="Type">{!obj.FileType}</div>
        </th>
        <td>
        </td>
    </tr>
</aura:iteration>


Please let me know if you have any other query. If this solution is helpful then please mark it as Best Answer.

Thanks,
Ankit Rathor 

All Answers

Ankit RathorAnkit Rathor
Hi Luciano,

I did walkthrough your code and found that the url which you are using for file download is not correct.

You can download file (Content Document) in lightning directly by using file id like that 
<a href = "BASE_URL/sfc/servlet.shepherd/document/download/File_ID" >Download</a>

Use This like 
<a href="{!'/sfc/servlet.shepherd/document/download/'+ v.Id}" target="_blank">DOWNLOAD</a>

Please try this code:
<aura:iteration items="{!v.files}" var="obj">
    <tr>
        <th scope="row"><a href="{!'/sfc/servlet.shepherd/document/download/'+obj.Id}" target="_blank">{!obj.Title}</a></th>
        <th scope="row">
            <div class="slds-truncate" title="Type">{!obj.FileType}</div>
        </th>
        <td>
        </td>
    </tr>
</aura:iteration>


Please let me know if you have any other query. If this solution is helpful then please mark it as Best Answer.

Thanks,
Ankit Rathor 
This was selected as the best answer
Luciano RobertoLuciano Roberto
Thanks Ankit Rathor  !!
It worked, I configured my code as follows:
 
<aura:iteration items="{!v.files}" var="obj">
               <tr>
                   <th scope="row">
                       <a href="{!'/portal/sfc/servlet.shepherd/document/download/'+obj.Id+'?operationContext=S1'}" target="_blank">{!obj.Title}</a>                       
                   </th>                   
                   <th scope="row"><div class="slds-truncate" title="Type">{!obj.FileType}</div></th>
               </tr>
</aura:iteration>

 
Ankit RathorAnkit Rathor
Thanks for choosing my answer as the best answer. Please do let me know if you need any further assistance.
Vani KumariVani Kumari
Hello @Ankit Rathor

I am also having a similar issue, Could you please help me, In the below link I have posted the issue

https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=9062I000000IZGmQAO