如何使用angular 2读写本地文件 [英] How to read and write local file using angular 2

查看:57
本文介绍了如何使用angular 2读写本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 angular 2 中,有没有办法从绝对路径读/写文件?
我使用了filesaver"库来保存文件,我在其中保存文件
本地为 txt/json 格式.
示例:

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");  

现在我想读取和写入/编辑 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?

推荐答案

要读取您的文件,您可以这样做(使用 HttpClient)

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});

}

不幸的是,您不能使用 put/post 写入文件,但我可以向您推荐 'jsonfile' 库:https://www.npmjs.com/package/jsonfile

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

希望能帮到你!

这篇关于如何使用angular 2读写本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆