问题
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?
回答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!
来源:https://stackoverflow.com/questions/48393628/how-to-read-and-write-local-file-using-angular-2