-1

I want to download a file using angular js... I got one link [It has code for downloading a file using Plunker][1[1]: Download a file with AngularJS angularjs/28873580#comment46050222_28873580

when i am trying to download it it says server not found...so i provided another link to download it, then it says download failed..no file there but file is there..

what i am doing wrong!!!!! controller

 var app = angular.module('plunker', []);
 app.controller('MainCtrl', function($scope) {
 $scope.fileHref = 'http://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf';
 });

front view

 <head>
 <meta charset="utf-8" />
 <title>AngularJS Plunker</title>
 <script>
 document.write('<base href="' + document.location + '" />');
 </script>
 <link rel="stylesheet" href="style.css" />
 <script data-require="angular.js@1.3.x" 
 src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14">
 </script>
 <script src="app.js"></script>
 </head>

<body ng-controller="MainCtrl">
<a ng-href="fileHref" download="yourFilename">Download</a>
</body>
</html>
Akshay Indalkar
  • 359
  • 2
  • 12

1 Answers1

1

this simplest angular page may help you,

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    </head>
    <body ng-app="test" ng-controller="myCtrl">
       <a ng-href="{{fileHref}}" >My PDF</a>

        <script>

            angular.module('test',[]).controller('myCtrl', function($scope) {
                $scope.fileHref = 'https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf';
            });


        </script>

    </body>
</html>
Azad
  • 5,144
  • 4
  • 28
  • 56
  • I tried your code...it is showing the pdf but not downloading into my disk..@azad – Akshay Indalkar Mar 19 '18 at 06:59
  • 1
    you have to manually download it to your PC otherwise GET file as buffer to your angular app and invoke some js download codes if you can access that pdf file without AJAX cross origin access violation – Azad Mar 19 '18 at 07:06