react-native-fs常用简单功能

目录

RNFS.MainBundlePath :IOS 的只读目录
RNFS.ExternalDirectoryPath :Android 的分享目录
RNFS.DocumentDirectoryPath :多平台适用的文档,可读写、删除

读取文档列表

RNFS.readDir(RNFS.DocumentDirectoryPath).then((result) => {
 let datas = [];
 result.forEach(i => {
   if (i.isFile()) {
     datas.push({title: i.name, url: i.path});
   }
 })
 console.log(datas);
 }).catch((err) => {
   console.log(err.message, err.code);
 });

下载文档列表

RNFS.downloadFile({
   fromUrl: encodeURI(url),
   toFile: RNFS.DocumentDirectoryPath + '/' + name
 }).promise.then(res => {
   console.log('downloadFile', res);
 }).catch(err => {
 console.log('downloadFile err', err);
 });

删除文档

const path = RNFS.DocumentDirectoryPath + '/test.txt';
RNFS.unlink(path).then(() => {
   let datas = [];
   datas = datas.concat(this.state.dataDownload);
   datas.splice(index, 1);
 }).catch((err) => {
   console.log(err.message);
 });