Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to download a file in angularjs controller

Tags:

angularjs

I want to download a file on button click in angularjs

 <button ng-click=downloadfile()>download</button>

In controller

  $scope.downloadfile=function()
        {
            //
        }

which functionality i have to write in downloadfile function could you please explain

like image 941
nagulu vemula Avatar asked Mar 08 '26 01:03

nagulu vemula


1 Answers

Write this out in your downloadFile() function.

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);        

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName + ".csv"; //this is an example file to download, use yours

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
like image 101
Yagiz Ozturk Avatar answered Mar 10 '26 15:03

Yagiz Ozturk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!