Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file from a url when a button is clicked in angular5 / 6

I have a url for ex: abc.net/files/test.ino The requirement is to download an .INO file through a button click event in angular 5 or 6

like image 724
Rushan De Silva Avatar asked Apr 09 '19 08:04

Rushan De Silva


1 Answers

you can create an anchor tag to download the file on button click event

downloadMyFile(){
    const link = document.createElement('a');
    link.setAttribute('target', '_blank');
    link.setAttribute('href', 'abc.net/files/test.ino');
    link.setAttribute('download', `products.csv`);
    document.body.appendChild(link);
    link.click();
    link.remove();
}

now call this function from your button

<button (click)="downloadMyFile()">download File<button>
like image 118
HUSSAIN Avatar answered Oct 18 '22 12:10

HUSSAIN