如何使用 react-native-fs 库访问文件和文件夹

HSB*_*SBP 1 react-native react-native-fs

我正在使用 react-native-fs 进行文件系统访问。但是我无法访问ios中的文件。

下面是代码:

RNFS.readDir(RNFS.MainBundlePath)
.then((result) => {
    console.log('Got result', result);
    return Promise.all([RNFS.stat(result[0].path), result[0].path]);
})
    .then((statResult) => {
        console.log('stat result',statResult);
        if(statResult[0].isFile()){
            return RNFS.readFile(statResult[1], 'utf8');
        }
        return 'no file';
    })
        .then((contents) => {
            console.log('Got Contents',contents);
        })
.catch((err) => {
    console.log(err.message, err.code);
})
Run Code Online (Sandbox Code Playgroud)

我正在获取 MainBundlePath 的路径文件,但没有获取本地存储的任何其他文件/文件夹。

HSB*_*SBP 5

回答我自己的问题。我终于能够使用 react-native-fs 库在 react native 中访问存储在 android 上的任何文件。

在给定的常量列表中尝试了ExternalStorageDirectoryPath

所以下面是代码(由 react-native-fs git repo 提供):

RNFS.readDir(RNFS.ExternalStorageDirectoryPath)
.then((result) => {
console.log('GOT RESULT', result);
return Promise.all([RNFS.stat(result[0].path), result[0].path]);
})
.then((statResult) => {
if (statResult[0].isFile()) {
return RNFS.readFile(statResult[1], 'utf8');
}
return 'no file';
})
.then((contents) => {
console.log(contents);
})
.catch((err) => {
console.log(err.message, err.code);
});
Run Code Online (Sandbox Code Playgroud)