How to read and write local file using angular 2

假装没事ソ 提交于 2019-12-11 06:45:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!