1

I want to download a PDF from URL received via $http GET() request from the server. The $http is initiated as soon as a button is clicked on the front-end. On the success of the $http is receive a URL of where the PDF file is saved and want it to download automatically on the success of the $http request. How do I achieve this ?

EDIT:

Here's the HTML code:

<div class="button clearfix">
  <button class="btn btn-primary pull-left m-b" ng-click="generateReport()">Download Report</button>
</div>

Here's my controller (Angularjs) code :

$scope.generateReport = function() {
        $http.get('/pdfreport').success(function(response) {
            $scope.url = response;
        }).error(function(response) {
            console.log("Failed");
        });
    }

I want to download file from $scope.url when the button is clicked

Chirag
  • 567
  • 5
  • 20

1 Answers1

0

Inside your success function create <a> element and assign your $scope.url (or reposnse data) to href attribute and then call click method of the created <a> element to download the file automatically:

  var createA = document.createElement("a")
  createA.setAttribute('href', $scope.url);
  createA.click();
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Malkhaz
  • 11
  • 7