React native 读取目录-Expo文件系统中的文件
我使用以下代码从API下载选定的PDF文档: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
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>
))}
这是一个基本的例子,可以帮助那些努力从目录文件夹直接获得循环的人。希望能有帮助