2

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

  • 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. – Santanu Halder Nov 14 '16 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 '16 at 13:49
  • Can you update the question with your relevant code of fetching URL as string, I can edit that. – Santanu Halder Nov 14 '16 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 '16 at 14:04
2

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");
    urlEvent.setParams({
      "url": b.getReturnValue()
    });
    urlEvent.fire();
}
| improve this answer | |
  • Glad I could help! – Santanu Halder Nov 14 '16 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. – Santanu Halder Apr 19 '17 at 6:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.