1

我必须将使用 react-native-document-picker 选择的文件复制到我的应用程序的临时路径,然后读取该文件,但是当它到达该await ReactNativeFS.copyFile(realPath, tempPath);行时,它永远不会解析。这是代码


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

1 回答 1

1

所以我发现 copyFile() 方法本身会解码路径,我只是将编码的 uri (与我从 document-picker pick() 方法接收的方式相同)作为参数传递,它工作得很好,谢谢。

searchAndReadFiles = async () => {
        try { 
            const fileSelected = await DocumentPicker.pick ({
                type: DocumentPicker.types.allFiles, 
            }); 
            const destPath = `${ReactNativeFS.CachesDirectoryPath}/${fileSelected.name}`;
            await ReactNativeFS.copyFile(fileSelected.uri, destPath);
            ...
于 2019-08-21T18:39:27.287 回答