+ Start a Discussion
Deepak Kumar SharmaDeepak Kumar Sharma 

Failed to download a file with anchor tag (<a></a>) in lightning component.

I created a lightning component that used an anchor tag for downloading vcf (or text) file but this is not working in Salesforce1 specially with iphone (ios version 12.4.1 as per salesforce norm). 

you can reproduce that from your side as well :-
  • Just create a component with anchor tag and try to download a simple text file from Component's JS Controller.
  • Then create quick action for that component.
  • Then drag and drop above quick action on page layout of any object.
  • Then login with salesforce1 app in iPhone (ios version > 12.0) and try to download a file on click of anchor tag.
  • Then you will see downloading is working for desktop but not in SF1 mobile app.

Here is sample code for this:-
>> Copy and paste following sample code in component :-
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="uriContent" type="String" default=""/>
<ui:button label="Download" class="slds-button slds-button--neutral" labelClass="label" press="{!c.download}"/>
    <a title="anchor" name="download!" href="{!v.uriContent}" id="test" download="Test.txt" aura:id="downloadLink" target="_blank" ></a>
>> Copy and paste following sample code in component's JS controller:-
download : function(component, event, helper) {
        var NameOfRecord = 'Test';
        component.set("v.uriContent","data:application/octet-stream," + 'This is my text File!!');
        //var link = document.getElementById("test");
        var link = component.find('downloadLink').getElement();
        link.href = component.get("v.uriContent");
        link.download = NameOfRecord+'.txt';

Thanks & Regards,