i.e., I have created this for my angular2 app. so this function is written in TypeScript
createDownloadLink(filename: string, data: string) {
var dataUri = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(data);
var anchor = document.createElement('a');
anchor.setAttribute('href', dataUri);
anchor.setAttribute('download', filename);
// anchor.appendChild(document.createTextNode("Click to download your file."));
// creates: <a href="data:application/octet-stream,DATA" download="FILENAME">TITLE</a>
return anchor;
}
createDownloadLink('hello.json', '{"name": "Hello World"}').click();
http://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser https://github.com/fhinkel/create-download-link