3

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.

HSBP
  • 735
  • 2
  • 8
  • 22

2 Answers2

10

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);
});
HSBP
  • 735
  • 2
  • 8
  • 22
  • Hi there, I am having to understand `react-native-fs` , I have implemented their basic example, but no page opens for user to select file from... is that having a different implementation, where user is able to browse files and select, then `react-native-fs` turn comes to upload ?? – Yasir Oct 24 '18 at 06:06
  • hi, HSBP can we get the file from a different app like I need to get skype_for_business app file into my project? – Yusuf Khan Apr 01 '19 at 09:27
4

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

Tim
  • 10,459
  • 4
  • 36
  • 47
  • But am getting empty array if i replace MainBundlePath with the above constant, i uploaded some files in icloud drive to test. – HSBP Nov 09 '17 at 08:58
  • you have to save files locally via react-native-fs' function "writeFile". After that you are able to read your saved files via "readFile". – Tim Nov 09 '17 at 09:01
  • No i am trying to access music files in ios which are locally stored. Can you tell me how do i do that? – HSBP Nov 09 '17 at 09:03
  • You want to access iTunes music files directly from your own app? – Tim Nov 09 '17 at 09:05
  • I have uploaded some music files in my icloud and now i want to access it through my app. – HSBP Nov 09 '17 at 09:09
  • with react-native-fs you can't access your icloud directly. You have to save your music files via your own app, to access them inside your app. – Tim Nov 09 '17 at 09:12
  • Oh thanksalot. So can i access the music files locally stored in android using same react-native-fs library? – HSBP Nov 09 '17 at 09:16