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
cyberdynebotcyberdynebot 

Excel download from API

I'm trying to create a Lightning component that uses a button to make an API call. The response is in Content Disposition form that returns an Excel file. I'd like to be able to have the Excel file begin downloading automatically to a User's local machine when the User clicks the button.

Not sure how to handle the Http Response to pass to the Lightning component to initiate the download. Any help is appreciated.

Lightning Component
<aura:component controller="ExcelHandler " implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:appHostable" access="global">

    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="workbookOption" type="String"/>
    <aura:attribute name="downloadLink" type="String"/>
 
    <lightning:card title="Workbook Download">    
            <p class="slds-p-horizontal_large">
                <lightning:select name="selectItem" label="Select a Template" value="{!v.workbookOption}">
                    <option value="">None</option>
                    <option value="Option1">Agency Screening Workbook</option>
                    <option value="Option2">Fannie Mae</option>
 
                </lightning:select>
                <lightning:button variant="brand" label="Create" onclick="{!c.callOutCtrl}" />
                
            </p>      
    </lightning:card>
</aura:component>

Javascript Controller

({
    callOutCtrl : function(component, event, helper) {
        var action = component.get("c.getFile");
        action.setParams({
                strOppId : component.get("v.recordId"),
                workbookOption : component.get("v.workbookOption")
        });
        action.setCallback(this, function(response){
            //Need to get returned Excel file from the Controller after making API call
        });
        
        $A.enqueueAction(action);
    }
})

Apex Controller

public class ExcelHandler {  
    
    @AuraEnabled
    public static void getFile(String strOppId, String workbookOption) {
    
        HttpRequest request = new HttpRequest();
        //Make API call

        //Found this online (https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000AY1qIAG)
        String base64value = EncodingUtil.base64Encode(response.getBodyasBlob());

        //Need to download Excel file on the Lightning Component
    }
}