react-native-blob-util
react-native-blob-util copied to clipboard
Multiple files created on downloading a single file - Android < 11 - copyToMediaStore
Version: 0.18.6 RN version: 0.71 Android: 9 (same issue on 8.1 too) Device: Nexus 6P emulator
Hello,
I am trying to download a file to the Downloads folder and am seeing three files after download. Two valid files, one with proper name and one with escaped chars. And the third file is a 0kb file
Code for downloading file:
if (Platform.OS === 'ios') { ReactNativeBlobUtil.ios.openDocument(response.data); } else { try { await ReactNativeBlobUtil.MediaCollection.copyToMediaStore( { name: docName, parentFolder: '', mimeType: mimeType, }, 'Download', response.path(), ); } catch (err) { console.log(err); }
The code is working fine for Android 11 and greater. Is there something I am doing wrong here? Thank you.
The code works for Android 9 if I change the save path to LegacyDownloadDir instead. Its just the copyToMediaStore that seems to have this issue.
The copy to mediastore should technically mimic the behavior of storing a file normally. So it looks like a bug
Thank you. Is there any other way to download a file to Downloads folder for older Androids? I mentioned earlier that I was able to use LegacyDownloadDir, but that only copies a binary file.
Basically something like this:
.config({
// response data will be saved to this path if it has access right.
path: dirs.LegacyDownloadDir+ '/path-to-file.anything',
transform: true
})
.fetch('GET', 'http://www.example.com/file/example.zip', {
//some headers ..
})
.then((res) => {
// the path should be dirs.LegacyDownloadDir+ 'path-to-file.anything'
console.log('The file saved to ', res.path())
})```
But that depends on the Android version if it works (scoped stroage and all these chagnes)
Thanks for the reply. I will have a look at this.