I developed custom files section using lightning components.
I am trying to download file from ligtning component, but not able to find solution.
Piece of component code:
<aura:attribute name="Baseurl" type="String" />
<a href="" data-id="{!account.acc.ContentDocumentId}" onclick = "{!c.downloadfile}" >Download</a>
Controller.JS
downloadfile : function (component, event, helper){
var id = event.target.getAttribute("data-id");
alert('Document ID:' +id);
var actiondownload = component.get("c.DownloadAttachment");
actiondownload.setParams({
"DownloadAttachmentID": id
});
actiondownload.setCallback(this, function(b){
component.set("v.Baseurl", b.getReturnValue());
});
Apex COntroller
public static string DownloadAttachment(Id DownloadAttachmentID)
{
ContentVersion oldCV = [Select Id,Title,Locked__c,Locked_By__c from ContentVersion Where ContentDocumentId=:DownloadAttachmentID and IsLatest = true ];
System.Debug('Old LV INitial :'+ oldCV.Id);
System.Debug('Old Lock :'+ oldCV.Locked__c);
String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm();
String URlDownload = fullFileURL+'/sfc/servlet.shepherd/version/download/'+oldCV.Id;
system.debug('Download URL:' +URlDownload);
return URlDownload;
}
Below is the piece of code used in classic salesforce
pagereference newTab=new pagereference('/sfc/servlet.shepherd/version/download/'+oldCV.Id+'?asPdf=false&operationContext=CHATTER');
newTab.setRedirect(true);
return (newTab);
i cannot use "pagereference" in @auraenabled. Is there any other option to download files from Apex controller ?
Thanks.
Best Regards
Visu
Best Answer
After the discussion in the comments of the question, here is how you can use navigateToURL event. Update your
setCallBack
to this.