我正在使用react-native-fetch-blob来下载mp3文件,然后再播放它们。问题是RNFetchBlob.fs.dirs.DocumentDir
的实际路径在我关闭应用程序并重新启动时发生了变化,这意味着我无法使用下载文件后保存的绝对文件路径在下次运行应用程序时检索文件。如何通过反应本机和react-native-fetch-blob在iOS上下载和检索本地文件?
这里是我的下载和存储的文件路径代码:
export function downloadAudio(urlArray) { //download every section in the urlArray
return (dispatch) => {
Promise.all(urlArray.map(section => {
dispatch(startDownload(section.section_id))
console.log('download start for id = ',section.section_id) //start download
return RNFetchBlob
.config({
path: RNFetchBlob.fs.dirs.DocumentDir + '/courseSections/' + section.section_id + '.mp3',
appendExt: 'mp3'
})
.fetch('GET', section.section_url, {
'Cache-Control': 'no-store'
})
.progress({ count: 10 }, (received, total) => {
console.log(`progress for section ${section.section_id}: `, Math.floor(received/total*100), '%')
dispatch(downloadProgress({id: section.section_id, progress: Math.floor(received/total*100)/100}))
})
.then(res => {
console.log(`section ${section.section_id} is saved to: `, res.path())
return { path: res.path(), id: section.section_id }
})
.then(pathInfo => dispatch(downloadSuccess(pathInfo))) //download finished
}))
.catch((error, statusCode) => {
console.log(statusCode, ' error: ', error)
})
}
}
我存储{ path: res.path(), id: section.section_id }
为持久的应用程序状态的一部分。但是下次打开应用程序时cuz没有用,文件路径已被重新分配。例如,这里的路径中的一个文件被下载到: downloaded file path
但经过我关闭该应用程序并重新启动,路径更改为: /Users/NatashaChe/Library/Developer/CoreSimulator/Devices/830778DE-3CE3-4FC7-AA4B-DFBAE999751C/data/Containers/Data/Application/0C47E33F-ACF8-4539-9456-8FF3E8FBECB2/Documents/courseSections/14.mp3
即文件已经分配到不同的子文件夹在Application
文件夹下。
显然,音频播放器(我使用的反应,本机的声音)可以使用原始路径,该应用程序已经存储了未找到该文件。所以我的问题是:有没有一种方法可以修复路径?或者有没有其他的方式来检索我不知道的文件?
还有,我用我的电脑上的iOS模拟器。还没有在手机上试过这个。有谁知道上述代码是否可以直接在iPhone上使用,并将文件保存到我的应用程序的目录?或者我应该用其他方式设置文件路径吗?
我使用反应原生0.41。