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.
To export package JSON first open a package, find the Import/Export dropdown and press the Export button. After that, the browser will start a download. The exported JSON file is formatted so it will be easy to track the difference between such files.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With