React-native-fs: RNFS.exists() returns True but RNFS.readFile fails with error: File doesn't exist

Created on 3 Jun 2018  Â·  12Comments  Â·  Source: itinance/react-native-fs

async _loadCustomData(fileName, fieldName) {
    const filePath = RNFS.ExternalStorageDirectoryPath + '/Download/' + fileName;

    if (await RNFS.exists(filePath)) {
      console.log("custom data file for " + fieldName + " exists");
      let customData = RNFS.readFile(filePath);
      customData = JSON.parse(customData);

      let newState = {};
      newState[fieldName] = customData;
      this.setState(newState);
    } else {
      console.log("No such file as ", fileName);
    }
 }

The if-condition evaluates to True and console.log displays that the file exists, but, I am unable to read the contents of the file.
customeData evaluates to an object like this {_45: 0, _65: 0, _55: null, _75: null}

So, to read this Object, I did this:

RNFS.readFile(filePath)
      .then((contents) => {
          console.log(contents);
          customData = JSON.parse(contents);

          let newState = {};
          newState[fieldName] = customData;
          this.setState(newState);
        })
        .catch((err) => {
          console.log(err.message, err.code);
        });

Now, I get the error: "ENOENT: no such file or directory, open '/storage/emulated/0/Download/chimp-list-additional.json' ENOENT"

I have tried the approach here https://github.com/itinance/react-native-fs/issues/471 but it doesn't work for me.

Please help!!

Most helpful comment

same issue on copyFile

RNFS.exists(sourceurl)
                .then(exist => {
                    if (exist) {
                        RNFS.copyFile(sourceurl, desPath)
                        .then((res) => {
                            copyResolve(res);
                        })
                        .catch((err) => {
                            copyResolve(err);
                        })
                    } else {
                        copyResolve(false);
                    }
                })

exists returns true , but copy prompt 'The file “0C635EA5-5BA8-4933-8C01-E570FB7EDACA.jpg” doesn’t exist.'

All 12 comments

same here.

Same here.
In my case, i check if the file exist, and it returns true, but when I use readFile and encode to base64 it returns a giant string that isn't a base64 file. It was working fine on react-native 0.48, and the problem started after I update to react-native 0.56.

var b64data = await RNFS.readFile(imagem, 'base64').then();

I need to use the b64data, but I'm facing the error, since is not a base64 file.

"dependencies": {
...
"react-native": "0.56.0",
"react-native-fs": "^2.11.15",
...
}

Same here
How can I solve ,pelease help

me too having the exact same problem ...

same here.
isFile()returns True but RNFS.readFile fails with error: File doesn't exist

Same here, any solution yet?

same problem exists with copyFile() too on iOS.

I did it with the below code:

if(await RNFS.exists(path)){
        setLoading(true)
        RNFS.readFile(path,'utf8')
        .then(data=>JSON.parse(data))
        .then(data=>setPeople(data))
        .then(()=> setLoading(false))
        .error(error=>setError(error))
        }

Hi,

Getting true on if (await RNFS.exists(filePath)) doesn't mean that the file exists.
Since it returns a Promise<boolean>, the if condition will always be true.

Try the following

RNFS.exists(filePath).then((status)=>{ if(status){ console.log('Yay! File exists') } else { console.log('File not exists') } })

@SrinathBose,
I've written code in below format where I'm checking if file exists then trying to copy the file to the destination even though I'm getting the same error i.e.

Error: The file “687CBFFD-7D05-4F51-A1CE-072BBF1CD001.jpg” doesn’t exist.

code:

RNFS.exists(sourcePath) .then(async (success) => { if (success) { console.log("Yay! File exists : ", success); await RNFS.copyFile(sourcePath, destinationPath); } }) .catch((err) => { console.log("Exists Error: ", err); });

Edit: earlier It worked for me but now facing this issue, It seems inconsistent.

same issue on copyFile

RNFS.exists(sourceurl)
                .then(exist => {
                    if (exist) {
                        RNFS.copyFile(sourceurl, desPath)
                        .then((res) => {
                            copyResolve(res);
                        })
                        .catch((err) => {
                            copyResolve(err);
                        })
                    } else {
                        copyResolve(false);
                    }
                })

exists returns true , but copy prompt 'The file “0C635EA5-5BA8-4933-8C01-E570FB7EDACA.jpg” doesn’t exist.'

same issue on copyFile

RNFS.exists(sourceurl)
                .then(exist => {
                    if (exist) {
                        RNFS.copyFile(sourceurl, desPath)
                        .then((res) => {
                            copyResolve(res);
                        })
                        .catch((err) => {
                            copyResolve(err);
                        })
                    } else {
                        copyResolve(false);
                    }
                })

exists returns true , but copy prompt 'The file “0C635EA5-5BA8-4933-8C01-E570FB7EDACA.jpg” doesn’t exist.'

same here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

duckmyanmar picture duckmyanmar  Â·  3Comments

let-aurn picture let-aurn  Â·  3Comments

PimDeWitte picture PimDeWitte  Â·  4Comments

npomfret picture npomfret  Â·  4Comments

CesarLanderos picture CesarLanderos  Â·  4Comments