wkh237/react-native-fetch-blob

Downloading using download manager, Cannot Delete the file

drblmb opened this issue · 10 comments

On Android, I downloaded a PDF file using Download Manager...then, when I delete the file later, it reports success, but the files are not being removed from the system. I can see them in Recent Files and Download History.

RNFetchBlob.session('pdf').dispose().then(() => { console.log('Succeeded'); }

I also tried just deleting the file with RNFetchBlob.fs.unlink, and it also reports Succeeded, but the file is still there. How do I really delete the files?

@drblmb , files created by download manager should be scanned again using fs.scanFile so that it removed from Android media database.

@wkh237 , can you give me an example of this? The examples of using scanFile show it resolving a promise that returns no values.

.then((res) => RNFetchBlob.fs.scanFile([ { path : res.path(), mime : 'audio/mpeg' } ]))
.then(() => {
// scan file success
})
.catch((err) => {
// scan file error
})

In my case, I was downloading using DownloadManager which returns a path. What does scanFile do?

Thanks!

@wkh237, or anyone please, can someone show me how to delete a file I've downloaded with download manager?

@drblmb , from my understanding if you're going to remove a file downloaded by Android Download Manager, you need to do these 2 things :

  1. remove the file using fs.unlink
  2. scan the path again (fs.scanFile)

fs.unlink says it worked, but then after I close my app, I'm able to go to download manager and still open the file. It isn't really deleted, so this issue cannot be closed yet

me too, unlink does not working..

Okay, just closed it due to there's no response. Reopen this one.

@drblmb , @rolldone , as the default location the Download Manager uses might be somewhere you're app does not have access right. I think you can set a custom download location for ensuring the app has access right to the file after it's completed. But keep in mind that the path must on external storage (like DCIMDir)

RNFetchBlob.config({
    addAndroidDownloads : {
      useDownloadManager : true,
      title : new Date().toLocaleString() + ' - #236 test.png',
      path : PathToTheFile
    }
  })

I've also added some error handling to Download Manager and fs API which should throw an error when the file is not deleted.

You can checkout the commit from branch issue-236.

Will add related information to the documentation, thanks 👍

@wkh237 It appears this is working well if I use external storage. Thank you!

This might help to delete pdf file.

        RNFetchBlob.fs.exists(this.path)
          .then((exist) => {
            if (exist) {
              RNFetchBlob.fs.unlink(this.path);
            }
          });