Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to make a file download from a url?

I am trying to download a file from a url but I cannot find how to do. Following is my code

<button onClick="fileDownload()">Download File</button>
fileDownload(){
 let downloadableLink = 'www.abc.com/1235';
}

What to do next? the url returns a PDF file but how do I initiate the download?

2 Answers

check this out:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes

use an anchor tag like you would with a regular link, with the download attribute

yea that would work but I need a way to download it with button, that is where my brain start hurting and become numb.

put the button inside the anchor. for example, look at the email button in the code area here:

https://jamessouth.github.io/fifteenpuzzlegenerator/