I want to download a file in React Native and save it in a Directory selected by the User (e.g. Downloads).
Right now I'm using https://www.npmjs.com/package/react-native-document-picker to let the user pick a directory and https://www.npmjs.com/package/react-native-fs/v/1.2.0 to download the file.
Unfortunately, react-native-document-picker only gives me a dynamic URI to the directory, which does not exist when react-native-fs tries to access it. I think it's hidden behind something like a File-Provider.
I searched a lot but found no solution on how to get the real static path to a directory. Might be that I am missing something obvious, but I can not figure it out right now.
the code (simplified)
DocumentPicker.pickDirectory().then(pick => {
// this looks something like:
// file:///private/var/mobile/Containers/Shared/AppGroup/8443...EC/File%20Provider%20Storage/Downloads/
const pickedDirectory = pick.uri;
// resolves to false:
RNFS.exists(pickedDirectory)
// does not work
RNFS.downloadFile({
fromUrl: downloadUrl,
toFile: pickedDirectory + filename,
}).promise.then(r => { console.log(r)
});
Either we can opt for the solution give on the below link (using react-native-fs).
React native Unable to use local tmp file path for Image.source iOS
or try
Add copyTo:"documentDirectory" in the pick document options. Then in pickedDirectory.fileCopyUri you'll get the document path.
What happens is when we pick any Item from gallery or iOS file system, it picks and place it a temporary directory for some time, then it get auto removed from that directory. Try the above given solution, I believe It will work.