Download a CSV file from a lightning component using a button

auralightning-aura-componentsstatic-resources

I'm trying to add a button Download CSV Template where I'll connect the button with a static resource. So, when you click the button, the files downloads, user can open the file, fill and reupload to add products. Below is the screenshot where it should come.
Location where I want the button

But I'm not sure how I should do. Below is my modified code.

<aura:component description="VirtualCartComponent"
            controller="VirtualCartController"                                  implements="force:hasRecordId,force:lightningQuickAction,flexipage:availableForAllPageTypes">
<aura:attribute name="recordId" type="String" />
<aura:attribute name="columns" type="List" default="[]"/>
<aura:attribute name="data" type="ITC_VirtualCartLineItem__c"/>
<aura:attribute name="draftValues" type="Object" default="[]"/>
<aura:attribute name="selectedRows" type="List" default="[]"/>
<aura:attribute name="deleteRows" type="List" default="[]"/>
<aura:attribute name="quoteId" type="String" default=""/>
<aura:attribute name="retUrl" type="String" default=""/>
<aura:attribute name="loaded" type="Boolean" default="false"/>
<aura:attribute name="isReadOnly" type="Boolean" default="false"/>
<aura:attribute name="displayModal" type="Boolean" default="false" />
<aura:attribute name="channel" type="String" default="/event/VirtualCartEvent__e"/>
<aura:attribute name="subscription" type="Map"/>

<aura:handler name="init" value="{! this }" action="{! c.init }"/>
<aura:handler name="itemAddedEvent" event="c:VirtualCartEvent" action="{!c.handleVirtualCartEvent}"/>
<aura:handler event="force:refreshView" action="{! c.init }" />

<lightning:button name="Add Items" label="Add Items" disabled="{! v.isReadOnly }" iconName="action:edit" iconPosition="left" onclick="{!c.handleAddItems}"/>
<lightning:button name="Import CSV" label="Import CSV" disabled="{! v.isReadOnly }" iconName="action:upload" iconPosition="left" onclick="{!c.handleImportCsv}"/>
<lightning:button name="Process" label="Process" disabled="{! v.isReadOnly }" variant="success" iconName="action:approval" iconPosition="left" onclick="{!c.handleProcess}"/>
<lightning:button name="DownloadCSVTemplate" label="Download CSV Template" disabled="{! v.isReadOnly }" iconName="action:arrow_bottom" iconPosition="left" >
    <img src="/resource/CSV_Products_Template/application/vnd.ms-excel.csv" />
    <!--<a href="{!'/servlet/servlet.FileDownload?file=0815t0000008SngAAE'}" target="_blank"/>-->   </lightning:button>
<lightning:button name="Delete" label="Delete" aura:id="deleteButton" disabled="{! v.isReadOnly }" class="hide" type="submit"
                  variant="destructive" iconName="utility:delete" iconPosition="left" onclick="{!c.handleMassDelete}"/>
<br/>
<br/>
<aura:if isTrue="{! v.loaded }">

    <aura:set attribute="else">
        <lightning:spinner alternativeText="Loading" />
    </aura:set>
</aura:if>
<lightning:overlayLibrary aura:id="overlayLib"/>
<lightning:empApi aura:id="empApi"/>
<force:recordData aura:id="recordLoader"
                  recordId="{!v.recordId}"
                  fields="ITC_Processed__c"
                  recordUpdated="{! c.reloadView }"/>
<aura:if isTrue="{!v.displayModal}">
    <div>
        <section role="dialog" tabindex="-1" aria-label="Meaningful description of the modal content" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
            <div class="slds-modal__container">
                <header class="slds-modal__header">
                    <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" title="Close" onclick="{!c.closeBtn}">
                        <lightning:icon iconName="utility:close" size="small" variant="neutral"/>
                        <span class="slds-assistive-text">Close</span>
                    </button>
                </header>

                <div class="slds-modal__content slds-p-around_medium alignLeft" id="modal-content-id-1">
                    <p>Are you sure you want to process the virtual cart?</p>
                </div>
                <footer class="slds-modal__footer">
                    <lightning:button label="Yes"
                                      variant="brand" class="slds-m-top--medium"
                                      onclick="{!c.handleProcess}"/>
                    <lightning:button label="No"
                                      variant="brand" class="slds-m-top--medium"
                                      onclick="{!c.noBtn}"/>
                </footer>
            </div>
        </section>
        <div class="slds-backdrop slds-backdrop_open"></div>
    </div>
</aura:if>
<div style="min-height: 300px">
    <lightning:datatable
            enableInfiniteLoading="true"
            keyField="Id"
            columns="{! v.columns }"
            data="{! v.data }"
            draftValues="{! v.draftValues }"
            hideCheckboxColumn="{! v.isReadOnly }"
            selectedRows="{! v.selectedRows }"
            onsave="{! c.handleSaveEdition }"
            onrowaction="{! c.handleRowAction }"
            onrowselection="{! c.handleRowSelection }"
            showRowNumberColumn="false"/>
</div>
</aura:component>

I have not done any changes in Controller classes

Best Answer

You can get the url of a static resource in aura using $Resource.resourceName

It's probably easiest to create a link with <a href="{!$Resource.MyCsvFile}" download>Download CSV Template</a> and use css to make the link look like a button.