3

我可以使用以下功能读取目录中的文件

    const path = RNFS.DocumentDirectoryPath;

    RNFS.readDir(path)
    .then((result) => {
        console.warn(result);
        const Files = [];

        if(result != undefined && result.length > 0){

            for(index in result) {
                const item = result[index];
                console.log(item);

                if(item.isFile()){

                    Files.push(item);
                }
            }

            if(Files.length > 0) {

                callback(Files);
            }
            else{

                callback(null);
            }
        }
        else{
            callback(null);
        }
    })
    .catch(err => {

        console.log('err in reading files');
        console.log(err);
        callback(null);
    })

但是我想读取目录及其子目录和文件属于它们,有什么办法可以实现吗?

4

1 回答 1

2

我已经实现了从资产中的某些内容到文档目录中的某些内容的递归复制,如下所示:

import FileSystem from "react-native-fs";

async copyRecursive(source, destination) {
    console.log(`${source} => ${destination}`);
    const items = await FileSystem.readDirAssets(source);

    console.log(`mkdir ${destination}/`);
    await FileSystem.mkdir(destination);

    await items.forEach(async item => {
      if (item.isFile()) {
        console.log(`f ${item.path}`);

        const destPath =
          FileSystem.DocumentDirectoryPath + "/" + source + "/" + item.name;

        console.log(`cp ${item.path} ${destPath}`);
        await FileSystem.copyFileAssets(item.path, destPath);
      } else {
        console.log(`d ${item.path}/`);

        // Restart with expanded path
        const subDirectory = source + "/" + item.name;
        const subDestination = destination + "/" + item.name;
        await this.copyRecursive(subDirectory, subDestination);
      }
    });

    this.setState({
      androidReady: true
    });
}
于 2019-09-02T12:34:18.350 回答