Home > Mobile >  create file json or text in angular
create file json or text in angular

Time:05-03

i am trying to create a file .json or .text in angular and put it in path : /assets.

after searching for a while all result is to get data from file:

we can get data from file using this methode:

this.httpClient.get("assets/data.json").subscribe(data =>{
  console.log(data);
})

But how to post and create a new file data.json and put it in assets?

CodePudding user response:

Browsers aren't allowed to simply write to files to the server. If that was possible, the Google homepage would be vandalised 30 times a second.

You need to write a web service (in the programming language of your choice, which could be JavaScript or TypeScript if you arrange to have a server on which you can use Node.js).

You probably want to write the data somewhere other than assets (which sounds like a directory that is part of the built Angular application and not a user data directory) and you probably should be using a database rather than using JSON file (databases are efficient and deal with simultaneous writes easily) — your web service can have another endpoint that reports the contents of the database as JSON.

Once you have a web service, then you can make a POST or PUT request to the URL for it using @angular/common/http as described in the Angular guide.

  • Related