How to Write data to file in angularJS

Amrut picture Amrut · Feb 16, 2015 · Viewed 16.7k times · Source

I have a variable with huge data some thing like this.

I want to save it to JSON file, when I click a button or something.

Answer

user3497373 picture user3497373 · Apr 28, 2017

$scope.employees is the object to be saved to a file

$scope.savedJSON = angular.toJson($scope.employees,true);
var blob = new Blob([$scope.savedJSON],{
   type: "application/json;charset=utf-8;"
});
var downloadLink = document.createElement('a');
downloadLink.setAttribute('download', 'sampleJSON.txt');
downloadLink.setAttribute('href', window.URL.createObjectURL(blob));
downloadLink.click();