Username
Username

Reputation: 3663

How do I download a ZIP file with ngClick?

I have an AngularJS function that I'm trying to make download a file by sending a GET request to /download?id=10.

$scope.download = function(){
  $http({
    url: "/download",
    method: "GET",
    params: {id:"10"} 
  });
}

This function is bound to a button tag: <button ng-click="download()">DOWNLOAD</button>.

When I click the button, nothing happens. But if I navigate my browser to /download?id=10, it downloads the zip file that was made in my backend.

How do I make the ng-click method download the file?

Upvotes: 1

Views: 911

Answers (3)

Ved
Ved

Reputation: 12103

You forgot to add method:GET or POST in your function

  $scope.download = function(){
      $http({
        url: "/download",
        method:'GET'
        params: {id:"10"} 
      });
    }

For dynamic params:

 $scope.download = function(id){
  $http({
    url: "/download",
    method:'GET'
    params: {id:id} 
  });
}

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222682

You do not need to have a ng-click for a button, you can just replace with <a> element and add the href property,

<a href="/download?id=10" download>Download</a>

If you still want to have a button, use GET method to get the file,

$scope.download = function(){
      $http({
        url: "/download",
        method:'GET'
        params: {id:"10"} 
      });
    }

Upvotes: 1

Dinesh undefined
Dinesh undefined

Reputation: 5546

You could try like this

$scope.download = function(){

       window.loaction.href = "/download?id=10";

}

or

You can use

<a href="/download?id=10" download>Download</a>

Upvotes: 2

Related Questions