itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.94k stars 977 forks source link

RNFS.exists() returns false but RNFS.writeFile() gives ENOENT: open failed: EEXIST (File exists) #1182

Open ssorc3 opened 1 year ago

ssorc3 commented 1 year ago

This only happens on some specific devices. Vanilla Android seems to work fine but I have seen this issue on OnePlus and Samsung devices. The user downloads a file and deletes it using the file manager, then tries to download it again.

The gist of the code is this:

const ext = "pdf";
let uniquePath = `${RNFS.DownloadDirectoryPath}/myFile`;
while(await RNFS.exists(${uniquePath}.${ext}`)) {
   uniquePath = `${uniquePath} (${n++})`;
}

const filePath = `${uniquePath}.${ext}`;
// Log for sanity
console.log("File exists?", await RNFS.exists(filePath)); // This line logs 'File exists? false'
await RNFS.writeFile(filePath, contents, 'base64'); // Throws exception ENOENT: open failed: EEXIST (File exists)

The main device I am seeing this on is a OnePlus 7 Pro running Android 11.

vatsalsoni1302 commented 1 year ago

This only happens on some specific devices. Vanilla Android seems to work fine but I have seen this issue on OnePlus and Samsung devices. The user downloads a file and deletes it using the file manager, then tries to download it again.

The gist of the code is this:

const ext = "pdf";
let uniquePath = `${RNFS.DownloadDirectoryPath}/myFile`;
while(await RNFS.exists(${uniquePath}.${ext}`)) {
   uniquePath = `${uniquePath} (${n++})`;
}

const filePath = `${uniquePath}.${ext}`;
// Log for sanity
console.log("File exists?", await RNFS.exists(filePath)); // This line logs 'File exists? false'
await RNFS.writeFile(filePath, contents, 'base64'); // Throws exception ENOENT: open failed: EEXIST (File exists)

The main device I am seeing this on is a OnePlus 7 Pro running Android 11.

Same error any solution?

Viplove09 commented 1 year ago

I am also facing the same Issue on Samsung Galaxy M146B-Android 13.0

react-native: "0.71.8" react-native-fs: "2.20.0"

Has anyone been able to figure out the solution?

jihadrobb commented 8 months ago

I had the same issue, then I switched to https://github.com/joltup/rn-fetch-blob with exact same logic. My simple code:


export default async function saveBase64asImage(base64Data, fileName) {
  try {
    const filePath = `${RNFetchBlob.fs.dirs.DownloadDir}/${fileName}`;
    const fileExists = await RNFetchBlob.fs.exists(filePath);

    if (fileExists) {
      await RNFetchBlob.fs.unlink(filePath);
    }

    await RNFetchBlob.fs.writeFile(filePath, base64Data, 'base64');

    return filePath;
  } catch (error) {
    console.error('Error saving image:', error);
    throw error;
  }
}
pavelustenko commented 4 months ago

Have the same issue with both RNFS and RNFB. Samsung A31 Android 12. RNFS.exists(dest) returns false, but RNFS.copyFile(src, dest) throws an error that dest is already exists.