I am using angular to create an application and has a requirement to store a json object currently stored in a variable to file.json located in src/app/assets using angular. I have searched a lot and have not found a way to do this. Ask if need any more information.
-
1So, are you trying to read and write the JSON file? I'm afraid you'll not be able to perform the write operation. That's how the browser works.WSD– WSD2020-06-06 16:20:51 +00:00Commented Jun 6, 2020 at 16:20
-
You can do it using nodejs writefile nodejs.org/api/…ferhado– ferhado2020-06-06 16:21:09 +00:00Commented Jun 6, 2020 at 16:21
-
1@Ferhado not in the frontend (he is using Angular)WSD– WSD2020-06-06 16:22:22 +00:00Commented Jun 6, 2020 at 16:22
-
You are wright, not in frontend, I forgot about that, thanks!ferhado– ferhado2020-06-06 16:25:41 +00:00Commented Jun 6, 2020 at 16:25
-
@JoseGuzman file already exists in the assets folder i only need to update it or change the contents of it is there any workaround for this?Nnnnn– Nnnnn2020-06-06 16:26:33 +00:00Commented Jun 6, 2020 at 16:26
|
Show 2 more comments
1 Answer
You cannot write files with Angular. Don't forget that the Angular app is not running in the directory structure you create. It's not even running on a server. It's running in the browser as compiled JavaScript. It has no direct write access to any filesystem.
If you need to write to server-side files in your application, you need some server-side code. This can be achieved, for example, with NodeJS (Express, NestJS…) if you want to stick with JavaScript. Either way, you can't write files directly with Angular.