Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  133] [ 5]  / answers: 1 / hits: 31548  / 7 Years ago, tue, february 21, 2017, 12:00:00

I am using angular 2. I want to give functionality to download JSON file.



Like i have response with res = {bar : foo} then i want to create json file which will contain this response which can be download on button/anchor click.



Any help will be greatly appreciated.


More From » json

 Answers
62

It was simpler than expected.



constructor(private sanitizer: DomSanitizer){}

generateDownloadJsonUri() {
var theJSON = JSON.stringify(this.resJsonResponse);
var uri = this.sanitizer.bypassSecurityTrustUrl(data:text/json;charset=UTF-8, + encodeURIComponent(theJSON));
this.downloadJsonHref = uri;
}


in the template include



<a class=btn btn-clear title=Download JSON [href]=downloadJsonHref download=download.json></a>

[#58847] Sunday, February 19, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samara

Total Points: 326
Total Questions: 106
Total Answers: 103

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;