android / phonegap未捕获TypeError:无法读取文件中未定义的属性'downloader'

时间:2012-08-22 16:24:07

标签: android cordova phonegap-plugins

$(document).ready(function() {
window.plugins.downloader.downloadFile("http://some_path/images/image1.jpg", {overwrite: true}, 
      function(res) {
        alert(JSON.stringify(result));
    }, function(error) {
        alert(error);
    }
); });

错误: - 未捕获的TypeError:无法读取文件的未定义属性'downloader':///android_asset/www/index.html:11

我已经包含了正确的js文件,并且按照正确的顺序仍然会收到此错误...

我甚至尝试用cordova替换所有对PhoneGap的调用......但它仍然会出现同样的错误,例如

cordova.addConstructor(function() {
cordova.addPlugin("Downloader", new Downloader());
//window.plugins.Downloader = new Downloader();
//PluginManager.addService("Downloader", "com.phonegap.plugins.downloader.Downloader"); 
});

2 个答案:

答案 0 :(得分:4)

如果仔细观察LogCat日志,您会注意到“downloader.js”文件中存在错误。它适用于旧版本的phonegap。我正在搜索如何解决与你相同的问题,但我只发现你的问题在这里发布。所以,底线是我们必须修复脚本文件中的语法。那些蠢货改变了整个语法而不考虑向后兼容性:(

为了使代码正常工作,请将downloader.js代码替换为:

/* downloader.js */
function Downloader() {}
    Downloader.prototype.downloadFile = function(fileUrl, params, win, fail) {

    //Make params hash optional.
    if (!fail) win = params;
       cordova.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]);
};

if(!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.Downloader) {
    window.plugins.Downloader = new Downloader();
}

接下来,电话应该是:

window.plugins.Downloader.downloadFile(url, {dirName: contentDirectory, fileName: someFileName, overwrite: true},
             function(data){
                 if(data=="exist"){
                  /// alert("File already exist");
                     console.log("File allready exist!");
                 }
                 else{
                     console.log("Status: " + data.status);
                 }
             },
             function(data){
                 console.log("error: "+data); 
             }
     );

答案 1 :(得分:0)

我知道这是一个老问题,但这是实际的,因为下载的phonegap实现效果不佳......

@Marjan:我尝试使用PhoneGap 2.8.1上的phonegap实现。我得到的最好的结果是工作下载,但所有下载的文件只有6千字节,并且没有任何错误。

所以...如果有人对下载器插件有所了解(如上所述),请告诉我们。