You need to sign in to do that
Don't have an account?
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
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; } }
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
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
It worked, I configured my code as follows:
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