简体   繁体   中英

Cordova: Uncaught ReferenceError: FileTransfer is not defined

I would like to download a PDF file from a server and save it to the local storage of that device. To do that I have found the cordova-plugin-file-transfer .

running that piece of code:

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://example.com/file.pdf");
var fileURL = "/Martin/Downloads/";

function openDocument() {
   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=="
        }
    }
);
}

gives me that issue:

Uncaught ReferenceError: FileTransfer is not defined

I have already removed all my platforms and reinstalled them. After that I have removed all my plugins and reinstalled them.

A similiar thread could not help me.

What else could I do ?

not the function but the init of the variables ran before device-ready was called.

here is the code that worked for me:

document.addEventListener("deviceready", onDeviceReady, false);

var fileTransfer;
var uri;
var fileURL;

// use file transfer after onDeviceReady() was called         
function onDeviceReady() {
    fileTransfer = new FileTransfer();
    uri = encodeURI('http://example.com/file.pdf');
    fileURL = '/file.pdf';
}

function openDocument() {

   fileTransfer.download(
    uri,
    fileURL,
    function(entry) {
        alert("download complete: " + entry.toURL());
        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);
        alert("download error source " + error.source);
    },
    false
);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM