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>


downloadfile : function (component, event, helper){                 
    var id = event.target.getAttribute("data-id");       
    alert('Document ID:' +id);
    var actiondownload = component.get("c.DownloadAttachment");

        "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');
        return (newTab);

i cannot use "pagereference" in @auraenabled. Is there any other option to download files from Apex controller ?


Best Regards Visu

  • Utilize getUrl() method of PageReference and return the URL as string in lightning component, then use navigateToURL event to navigate to the URL returned. I haven't tried this yet though. Nov 14, 2016 at 13:07
  • Thanks Santanu Halder. I have returned base url as string to lightning component. I need to call the navigateToURL event automatically on URL returned to lightning component.. How to achieve this, I am not familiar with events..
    – visu
    Nov 14, 2016 at 13:49
  • Can you update the question with your relevant code of fetching URL as string, I can edit that. Nov 14, 2016 at 13:54
  • Thanks Santanu Halder, i added piece of component code , controller.js and apex controller code (but not formatted correctly)..
    – visu
    Nov 14, 2016 at 14:04

1 Answer 1


After the discussion in the comments of the question, here is how you can use navigateToURL event. Update your setCallBack to this.

actiondownload.setCallback(this, function(b){
        component.set("v.Baseurl", b.getReturnValue());
var urlEvent = $A.get("e.force:navigateToURL");
      "url": b.getReturnValue()
  • Glad I could help! Nov 14, 2016 at 14:58
  • Since the answer has helped you, please accept this as answer and close this thread so that system doesn't treat it as unanswered one. Apr 19, 2017 at 6:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .