1
$(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);
    }
); });

错误:-未捕获的类型错误:无法读取文件中未定义的属性“下载器”:///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"); 
});
4

2 回答 2

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); 
             }
     );
于 2012-11-01T22:36:54.667 回答
0

I know this is an old question, but it is actual since the phonegap implementation of download doesn't work well...

@Marjan: I tried to work with the phonegap implementation on PhoneGap 2.8.1. The best result I got was a working download, but all downloaded files had only 6 kilobyte and there was no error at all.

So... If anybody has an idea on the downloader plugin to get it to work (as described above), please let us know.

于 2013-06-21T08:43:05.273 回答