Reputation: 1295
I have array items, Given below
writeFile1() {
this.testnames = [{name : "sheldon", age : "10"}, {name : "Simon", age : "11"}]
this.file.writeFile(this.file.tempDirectory, './assets/data/demo.json', JSON.stringify(this.testnames))
};
testnames:any;
I need to store "testnames" to the json file which is located at "assets/ data/ demo.json". so I dont know how to do! anyone know this please helpme out.
specification:
Ionic5
Angular 10
Upvotes: 0
Views: 79
Reputation: 6868
If you are using npm
package manager you can do it via file-saver
. Just do installation of file-saver
package :
npm install file-saver --save
Then import file-saver
accordingly:
import { saveAs } from 'file-saver';
const jsonObject: any = [{name : "sheldon", age : "10"}, {name : "Simon", age : "11"}];
const blob = new Blob([JSON.stringify(jsonObject)], {type : 'application/json'});
saveAs(blob, 'demo.json');
Keep in mind that web browser doesnt have the permission to write content to specified location. Instead what you can do is that you can create the File object in the browser iteself and download it accordingly. If it is a web browser it will be downloaded to your default downloading location. I'm providing a Angular based solution here since you have mentioned angular in your question.
Upvotes: 1