0

I have a JSON object in a controller. I need to be able to save that object to the file system. I am able to get text to save, but when I put a reference to the JSON object I am getting [object Object] 18 times (the number of items in the object). I need it to show the actual content.

html:

<input type="button" value="Download" ng-click="download('test.json', testMappings)"/>

Controller.js:

function download(filename, text){
  var element = document.createElement('a');
  element.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);
  element.click();
  document.body.removeChild(element);
}

'testMappings' is the name of the json object. I am using AngularJS v1.7.8. I work in an environment with limited ability to bring in new libraries. So I cannot just download file saver or any other library. Any ideas, references or links on how to save the object would be greatly appreciated.

0 Answers0