【问题标题】:Read files in directory - Expo FileSystem读取目录中的文件 - Expo FileSystem
【发布时间】:2020-05-27 01:41:24
【问题描述】:

我正在使用以下代码从 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.readDirectoryAsync(FileSystem.documentDirectory + 'app_docs'),但没有太多乐趣。

【问题讨论】:

    标签: react-native expo


    【解决方案1】:

    我通过以下方式解决了这个问题:

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

    这是一个基本示例,可帮助任何努力从目录文件夹中直接获得循环工作的人。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 2019-06-17
      • 1970-01-01
      • 2022-07-18
      • 2012-06-25
      • 2012-04-09
      • 2021-03-03
      • 2015-02-19
      相关资源
      最近更新 更多