org.apache.cordova.file-transfer how to abort download file

1.7k views Asked by At

Suppose the connection on mobile is weak and gets stuck, how can I interrupt a file transfer in progress before the loading spinner digs a hole on the screen?

doc here https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md#abort

According to documentation use of the abort() method should do it, but looks like no way. Both iOS and Android. Download function works. However the abort don't do anything. The file is downloaded after the abort() call, anyway. Even after 10 seconds.

I have:

function downloadMyFilePlease () {

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.onprogress = function(progressEvent) {
        //...
                                                        };

fileTransfer.download(
    uri,
    fileURL,
    function(entry) {
        console.log("download complete: " + entry.toURL());
    },
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }
);

}

function abortFileTrasfer () { // called on Abort button click

    fileTransfer.abort(); // <------ Why are you not working??? 
}

What am I missing?

1

There are 1 answers

4
Nick Pastuhov On

Variable fileTransfer isn't defined inside abortFileTrasfer() function, it's a local variable inside downloadMyFilePlease(), so you can't access it in abortFileTrasfer().