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
JeffreyStevensJeffreyStevens 

How to download attachment in lightning component

I have a lightning component that I'm putting on a Community page, and I want to place a download link on it - to download an attachment.

In Visualforce - I always did...
<apex:image value="/servlet/servlet.FileDownload?file={!attachmentId}"  />Download</apex:image>

So, in the markup of the component, I've tried
<a href="{!'/servlet/servlet.FileDownload?file=00P4D000000Oub6'}">Download</a>

But when I do this, and click on the link - it just's takes me to a Site is in Maintenance message. 

Has anybody done a download link of an attachment ID in a componet?

Thanks
Best Answer chosen by JeffreyStevens
JeffreyStevensJeffreyStevens
I found my answer.  I was running this in a Community, and I had to add the community name at the start of the link.  So, my a href is:  (team is the name of my community)

<a href="{!'/team/servlet/servlet.FileDownload?file=00P4D000000Ovli'}" target="_blank">Download</a>

All Answers

JeffreyStevensJeffreyStevens
I found my answer.  I was running this in a Community, and I had to add the community name at the start of the link.  So, my a href is:  (team is the name of my community)

<a href="{!'/team/servlet/servlet.FileDownload?file=00P4D000000Ovli'}" target="_blank">Download</a>
This was selected as the best answer
sonam singh 22sonam singh 22
Hi, I want to create a lightning component to display records alongwith the related attachments . There will be a column containing the icon for each record and on clicking the icon, it should download the respective attchments. Can anyone please help
Absar Ahmad 6Absar Ahmad 6
You can download file (Content Document) in lightning directly by using file id like that 
<a href = "BASE_URL/sfc/servlet.shepherd/document/download/File_ID" >Download</a>

Use This like 
<a href="{!'/sfc/servlet.shepherd/document/download/'+ v.Id}" target="_blank">DOWNLOAD</a>

 
Mahak_bMahak_b

We have the download attribute available for anchor tag. We can use that attribute to get the file downloaded directly. Assuming that you have to do it on a list of records received. Below is the answer.

<aura:iteration items="{!v.recordList}" var="item">
<a href="{!'/servlet/servlet.FileDownload?file='+{item.Id}}" target="_blank" download="{!item.Name}">Download</a>
</aura:iteration>
Medhavi SharmaMedhavi Sharma

Can we also use this <a href> for using the DistributionPublicUrl  from ContentDistribution?

Will_MWill_M
How can we download a field value (a long text area field type) as a download for VF pages rendered in Lightning? I was able to achieve this in Classic but in Lightning it does not work. Posted a question, would appreciate if anyone could provide any insight on the issue.
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000g7aZQAQ