Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  142] [ 3]  / answers: 1 / hits: 17086  / 7 Years ago, sun, june 18, 2017, 12:00:00

I need to generate the link and then download the link in one move. I have a button for generation and download and once is clicked i have to call the api to generate the download link for the pdf and then make the browser open the pop up with save item.



            <button type=button class=btn btn-success btn-block ng-click=generateAndDownloadPdf()><i
class=fa fa-plus></i>Generate and download invoice</span></button>


and my js controller function looks like :



            $scope.generateAndDownloadPdf = function(){
//getting download url from api
api.events.invoice.download($stateParams.eventId, $stateParams.speakerId).then(function(data){
console.log(data)
//give url to user

});

}


I know that once I have the download url I can use this html code, but was wondering if I can do all in one click for the user.



            <a
target=_self
href=download.url
download=file.pdf>
<button type=button class=btn btn-success btn-block ng-click=><i
class=fa fa-plus></i>Download Invoice</span></button>
</a>


Thank you all!


More From » angularjs

 Answers
13

you can force the download with this way in yout method generateAndDownloadPdf,



var anchor = document.createElement('a');
anchor.href = data.downloadUrl;
anchor.target = '_blank';
anchor.download = data.fileName;
anchor.click();

[#57402] Friday, June 16, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
susanajamiep questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Mon, Mar 7, 22, 00:00, 2 Years ago
Wed, Jun 10, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;