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.

2
  • You could follow this pattern by creating a new Blob and attching that to the href stackoverflow.com/a/43678525/1663821 Commented Jul 30, 2020 at 8:26
  • Thanks @Michael, creating a new Blob worked! I really appreciate it! :) Commented Jul 30, 2020 at 15:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.