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

查看:584
本文介绍了如何使用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'库:

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!

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

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