我使用rn-fetch-blob
从api下载文件。我可以看到它已成功下载,但是如您所知,在iOS平台上,每次访问文件系统时,目录路径都会更改。
但是当我转到设备的文件夹时,没有文件可以看到。如何下载并编写用户可以看到其设备的路径?
getFileName = async (url) => {
return await url.split('/api/files/others/')[1]
}
download = async () => {
let permissions = await this.requestCameraPermission()
if (!permissions && Platform.OS !== 'ios') {
Platform.OS === 'ios' ? RNToasty.Normal({ title: 'Storage permissions are required!' }) : RNToasty.Error({ title: 'Storage permissions are required!' })
return
}
try {
this.setState({ showDialog: true })
let file = this.props.navigation.state.params.article.files[0];
let fileName = await this.getFileName(file)
console.log({ fileName, file })
res = await RNFetchBlob.config({
fileCache: true,
path: Platform.OS === 'ios'? RNFetchBlob.fs.dirs.MainBundleDir + "/QCIPA/" + this.props.navigation.state.params.article.title + "/" + fileName : RNFetchBlob.fs.dirs.DownloadDir + "/QCIPA/" + this.props.navigation.state.params.article.title + "/" + fileName
})
.fetch('GET', Root + file + '?download=1')
.progress((received, total) => {
let downloadProgress = (received) / total
this.setState({ downloadProgress })
console.log({ downloadProgress, received, total })
})
Platform.OS === 'ios' ? RNToasty.Normal({ title: 'All Files Downloaded...' }) : RNToasty.Success({ title: 'All Files Downloaded...' })
this.setState({ showDialog: false })
console.log('The file saved to ', res.path(), { res });
this.setState({ showDialog: false })
} catch (error) {
Platform.OS === 'ios' ? RNToasty.Normal({ title: error.message }) : RNToasty.Error({ title: error.message })
this.setState({ showDialog: false })
}
}
static navigationOptions = ({ navigation }) => {
return {
header: (props) => <HeaderBackground {...props} />,
headerStyle: {
backgroundColor: Colors.transparent,
paddingTop: Platform.OS === 'ios' ? 0 : getStatusBarHeight(),
height: Header.HEIGHT + (Platform.OS === 'ios' ? 0 : getStatusBarHeight()),
},
title: navigation.state.params.article.title,
headerTintColor: Colors.white,
headerTitleStyle: {
fontWeight: 'bold',
padding: 5,
paddingTop: 10
},
headerMode: 'float',
headerLeft: <Icon
onPress={() => navigation.goBack()}
name="arrow-back"
type='MaterialIcons'
style={{ color: 'white', marginLeft: 10, padding: 5, paddingTop: 10 }}
/>,
}
}