We will modify the code from last week to add a button below each of the file names wwe retrieve from the API. when the button is clicked, another API call can be made to download the file.
The button function above, “downloadFile” takes the file’s name and makes a request to the API to get the file. The file is passed to the code as a binary stream. In order to have the file downloaded to the user’s device we have to employ a trick. We create a link element (<a>) which is a download type link. Link elements with a download attribute download the contents of HTTP response to the user’s device rather than opening them in the browser. The Code above “clicks” the link element and then removes it from the page, which downloads the file.
Now we have a list of files attached to the current record in the formula field with a functioning download button for each!