0

The file performs.json contains an JSON array with objects of user info.

I want to programmatically update this array and save again. But I don't know how.

$http.get('db/performs.json')
    .then(function (res) {
        res.data.push(newSubmit);
        // Save this new res.data to db/performs.json
    });
Engin
  • 755
  • 7
  • 20
  • Is that [tag:angularjs] (Version 1.x) or [tag:angular] (version 2+)? Please tag your post correctly – Alon Eitan Dec 06 '17 at 20:48
  • Don't you think that it would be a bad idea for anyone to be able to change files on your server with JS from their browser? That's why it's not possible. You'll need some kind of backend on your server that handles the file change/update. You can get the data to the server via an AJAX-Post – Luca Kiebel Dec 06 '17 at 20:49
  • 3
    You can't do that from the browser. It would be a huge security vulnerability if you could save files to the user's computer. If you just want to save some data you can use local or session storage. If you NEED to keep the file (add to database like you say) you must do that from the back end on your own server not the clients machine. – Drew Rochon Dec 06 '17 at 20:51
  • If the file is in your local computer, it is possible to "download" the modified file to replace the original one. – Anthony C Dec 06 '17 at 21:22

1 Answers1

0

When you call $http.get the server sends the file to you, it is then placed in memory, which you then modify. At this point you need to send the modified file back to the server for storage.

This needs to be supported by the server that served the file initially.

Here's an example of how to do in a NodeJS server

Joel Hernandez
  • 1,817
  • 4
  • 18
  • 27