1

In angular 2, is there any way to read/write files from absolute path?
I have used 'filesaver' library to save the file, where I save the file
locally in txt/json format.
Example :

let blob = new Blob([document.getElementById('exportFile').innerHTML],{ 
    type: "text/plain;charset=utf-8"
});
saveAs(blob, "export.json");  

Now I want to read and write/edit export.json file. How can I refer it for the next time?
Is there any other way or is there any good library available for these
operations?

2
  • Why are you saving the file under json format ? Commented Jan 23, 2018 at 10:14
  • Requirement is, I should save the admin dashboard layout data in some file. Where the widgets are movable, after widgets change there position I need to store the widgets css in some object and inturn storing that in json file. when user visits again he should get his modified layout. Basically I was asked to implement it locally by storing the data in some file. If you have better approach for this please help Commented Jan 24, 2018 at 4:29

1 Answer 1

1

To read your file you can do this (using HttpClient)

public getJSON(): Observable<any> {
         return this.http.get("./export.json")
                         .map(res => {var data = res.json(); return data});

}

Unfortunately you cannot use put / post to write into the file, but I can recommend you 'jsonfile' library : https://www.npmjs.com/package/jsonfile

Hope it will help you!

Sign up to request clarification or add additional context in comments.

3 Comments

I am unable to use this library, how to import this?
Npm install —save jsonfile
Then use require statement to import in your code jsonfile

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.