Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 读取目录-Expo文件系统中的文件_React Native_Expo - Fatal编程技术网

React native 读取目录-Expo文件系统中的文件

React native 读取目录-Expo文件系统中的文件,react-native,expo,React Native,Expo,我使用以下代码从API下载选定的PDF文档: DownloadDocument = (docUri) => { FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'app_docs/', { intermediates: true }); FileSystem.downloadAsync( ' https://example.com/library/documents/' + docUri, FileSys

我使用以下代码从API下载选定的PDF文档:

DownloadDocument = (docUri) => {

FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'app_docs/', { intermediates: true });

FileSystem.downloadAsync(
  ' https://example.com/library/documents/' + docUri,
  FileSystem.documentDirectory + 'app_docs/' + docUri
)
  .then(({ uri }) => {
    console.log('Finished downloading to ', uri);
  })
  .catch(error => {
    console.error(error);
  });

}
是否有一种方法可以循环下载到app_docs文件夹中的所有文档


我一直在尝试使用FileSystem.readdirectoryancfilesystem.documentDirectory+“app\u docs”,但没有多少乐趣。

我通过执行以下操作解决了这个问题:

state = {
  docsList: [],
}

 componentDidMount() {
   this._getAllFilesInDirectory();
 }

_getAllFilesInDirectory = async() => {
   let dir = await FileSystem.readDirectoryAsync(FileSystem.documentDirectory + 'app_docs');

   dir.forEach((val) => {
     this.state.docsList.push(FileSystem.documentDirectory + 'app_docs/' + val);
});

   await this.setState({docsList: this.state.docsList, loading: false});
}
然后在渲染中:

    {this.state.docsList.map((val, key) => (
        <Text key={key}>{val}</Text>
    ))}
这是一个基本的例子,可以帮助那些努力从目录文件夹直接获得循环的人。希望能有帮助