How to access files and folders using react-native-fs library

笑着哭i 提交于 2019-12-11 05:07:01

问题


I am using react-native-fs for file system access. However am unable to access files in ios.

Below is the code:

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

I am getting path files for MainBundlePath but not any other files/folders which are locally stored.


回答1:


Answering my own question. I was finally able to access any files stored on android in react native using react-native-fs library.

Tried ExternalStorageDirectoryPath as in the list of given constants.

So below is the code (Provided by 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);
});



回答2:


I had the same issue. I think you have to use "RNFS.DocumentDirectoryPath", because you do not have full access to the MainBundle.



来源:https://stackoverflow.com/questions/47197227/how-to-access-files-and-folders-using-react-native-fs-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!