Cordova: Uncaught ReferenceError: FileTransfer is not defined

Cordova: Uncaught ReferenceError: FileTransfer is not defined

我想从服务器下载 PDF 文件并将其保存到该设备的本地存储中。 为此,我找到了 cordova-plugin-file-transfer.

运行那段代码:

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=="
        }
    }
);
}

给我那个问题:

Uncaught ReferenceError: FileTransfer is not defined

我已经删除了所有平台并重新安装了它们。 之后,我删除了所有插件并重新安装了它们。

类似的 thread 帮不了我。

我还能做什么?

不是函数而是变量的初始化 运行 在调用设备就绪之前。

这是对我有用的代码:

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
);
}