Search code examples
react-nativereact-native-fs

React native FS copyFile() never resolves


I have to copy a file picked with react-native-document-picker to the temporary path of my app and then read this file, but when it reaches the await ReactNativeFS.copyFile(realPath, tempPath); line, it never resolves. Here's the code


searchAndReadFiles = async () => {
  try { 
     const fileSelected = await DocumentPicker.pick ({
        type: DocumentPicker.types.plainText,
     }); 
     const decodedURI = decodeURIComponent(fileSelected.uri);
     const split = decodedURI.split('/');
     const name = split.pop();
     const inbox = split.pop();
     const realPath = `${ReactNativeFS.TemporaryDirectoryPath}/${name}`;
     const tempPath = `${ReactNativeFS.ExternalStorageDirectoryPath}/${fileSelected.name}`;
     await ReactNativeFS.copyFile(realPath, tempPath);
     const fileRead = await ReactNativeFS.readFile(tempPath);
  } catch (err) {   
     console.warn(err);
  }
}

Solution

  • So I discovered that the copyFile() method by itself decodes the path, and I just passed the coded uri (the same way as I was receiving from document-picker pick() method) as argument and it worked fine, thank you.

    searchAndReadFiles = async () => {
            try { 
                const fileSelected = await DocumentPicker.pick ({
                    type: DocumentPicker.types.allFiles, 
                }); 
                const destPath = `${ReactNativeFS.CachesDirectoryPath}/${fileSelected.name}`;
                await ReactNativeFS.copyFile(fileSelected.uri, destPath);
                ...