Skip to content

[Tip] How to Immediately delete canceled files during download + Android Task.cancel() not working with catch error #182

Closed
@bang9

Description

@bang9

Nevertheless you cancel the download task, file is exist exactly in your app caches

this.downloadTask
    .config({
        fileCache : true,
        path : RNFetchBlob.fs.dirs.DocumentDir + `/yourApp/${fileUniqueId}.mp3`
    })
    .fetch('GET', downloadURL, {});

this.downloadTask
    .progress({ interval : 100 }, (received, total) => {
        console.log('progress ' + Math.floor(received/total*100) + '%');
        progressCb(Math.floor(received/total*100))
    })
    .then(async (res)=>{
        console.log("then response",res);
        console.log('The file saved to ', res.path());
        const filePath = 'file://' + res.path();

       //save file path in AsyncStorage
        let saveList = {};
        const downloadList = await AsyncStorage.getItem("@download");
        if(downloadList)
            saveList = JSON.parse(downloadList);
        saveList[contentKey] = filePath;

        return AsyncStorage.setItem("@download",JSON.stringify(saveList))
    })
    .then(()=>{
        finishCb();
    })
    .catch((err)=>{
       //you must add this line. If you don't add, your app users waste they're storage
        RNFetchBlob.fs.unlink(RNFetchBlob.fs.dirs.DocumentDir + `/yourApp/${fileUniqueId}.mp3`);
        console.log(err)
    })

Android side download task cancel is not working

You can find solution in this PR(past repo), It works well for me
wkh237#568

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions