Is it possible to write data to a locally json file with nothing but angular?

Is it possible to write data to a locally json file with nothing but angular?

No. Even if you’re running the page from the local file system (e.g., file://myfile.html) or from a local webserver (e.g., http://localhost/myfile.html or http://host-on-my-intranet/myfile.html), you still have no means of directly writing to a file from browser-hosted JavaScript code.

Two choices:

  1. Send it to something (e.g., a server) that can write it out, or

  2. Provide it as a data: URI (if feasible in your case) that the user can right-click and choose “save as…”

    Here’s how you create a data: URI for some JSON text:

    var uri = "data:application/json;charset=UTF-8," + encodeURIComponent(theJSON);
    

Full Example of #2:

var theData = {
  foo: "bar"
};
var theJSON = JSON.stringify(theData);
var uri = "data:application/json;charset=UTF-8," + encodeURIComponent(theJSON);

var a = document.createElement('a');
a.href = uri;
a.innerHTML = "Right-click and choose 'save as...'";
document.body.appendChild(a);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)