uniapp 读取本地文件
介绍
开发中需要将一些固定配置保存到一个配置文件中,在static 下面创建了一个data.json文件
uniapp 有时候可能需要读取本地的json配置文件或者其他一些文件,晚上找了几个教程不管用,最后还是在官网找到了介绍
实现代码
read() {var _that = thisplus.io.resolveLocalFileSystemURL('/static/data.json', function(entry) {entry.file(function(file) {var fileReader = new plus.io.FileReader();fileReader.readAsText(file, 'utf-8');fileReader.onloadend = function(evt) {console.log(evt.target.result);_that.list = JSON.parse(evt.target.result)fileReader.abort()}});}, function(e) {console.log("Resolve file URL failed: " + e.message);});},
代码很简单,就不做介绍了
官网介绍
uni.getFileSystemManager() | uni-app官网 (dcloud.net.cn)
h5产业联盟介绍
HTML5+ API Reference (html5plus.org)
具体介绍都能看到,有代码和示例很简单