0

我从此链接下面的代码http://weblogs.asp.net/soever/cordova-file-transfer-unzip-and-present-adventures根据我的需要更改了URL。点击“加载”按钮没有触发事件,LocalFileSystem也没有定义

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<title>DemoZipUnzip</title> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"> 
<!-- DemoZipUnzip references --> 
<link href="css/index.css" rel="stylesheet" /> 
<!-- Cordova reference, this is added to your app when it's built. --> 
<!--<script src="cordova.js"></script>--> 
<script src="scripts/platformOverrides.js"></script> 
<script src="scripts/index.js"></script> 
</head> 
<body> 
<button id="btnLoad">Load</button> 
<button id="btnUnzip">Unzip</button> 
<hr /> 
<div id="statusPlace"></div> 
<hr /> 
<img id="imgPlace" src="http://lorempixel.com/320/200"> 
<br /> 
<div id="txtPlace">TEXT COMES HERE</div> 
<script type="text/javascript"> 
document.addEventListener("deviceready", onDeviceReady, false); 

function registerHandlers() { 
    document.getElementById("btnLoad").onclick = function() { 
     var that = this, 
       App = new DownloadApp(), 
       fileName = "Archive.zip", 
       uri = "http://localhost:63961/check/contentupdate/availableupdates", 
       folderName = "content"; 
     console.log("load button clicked"); 
     document.getElementById("statusPlace").innerHTML += "<br/>Loading: " + uri; 
     App.load(uri, folderName, fileName, 
       /*progress*/function (percentage) { document.getElementById("statusPlace").innerHTML += "<br/>" + percentage + "%"; }, 
       /*success*/function (entry) { document.getElementById("statusPlace").innerHTML += "<br/>Zip saved to: " + entry.toURL(); }, 
       /*fail*/function() { document.getElementById("statusPlace").innerHTML += "<br/>Failed load zip: " + that.uri; } 
     ); 
    }; 
    document.getElementById("btnUnzip").onclick = function() { 
     var that = this, 
       App = new DownloadApp(), 
       fileName = "Archive.zip", 
       folderName = "content"; 
     console.log("zip button clicked"); 
     App.unzip(folderName, fileName, 
       /*success*/function() { alert("Unzipped and assigned"); }, 
       /*fail*/function (error) { alert("Unzip failed: " + error.code); } 
     ); 
    }; 
} 

function onDeviceReady() { 
    // navigator.splashscreen.hide(); 
    document.getElementById("statusPlace").innerHTML += "<br/>deviceready event received"; 
    registerHandlers(); 
} 

var DownloadApp = function() { 
} 

DownloadApp.prototype = { 
    load: function (uri, folderName, fileName, progress, success, fail) { 
     var that = this; 
     that.progress = progress; 
     that.success = success; 
     that.fail = fail; 
     filePath = ""; 

     that.getFilesystem(
       function (fileSystem) { 
        console.log("GotFS"); 
        that.getFolder(fileSystem, folderName, function (folder) { 
         filePath = folder.toURL() + "/" + fileName; 
         that.transferFile(uri, filePath, progress, success, fail); 
        }, function (error) { 
         console.log("Failed to get folder: " + error.code); 
         typeof that.fail === 'function' && that.fail(error); 
        }); 
       }, 
       function (error) { 
        console.log("Failed to get filesystem: " + error.code); 
        typeof that.fail === 'function' && that.fail(error); 
       } 
     ); 
    }, 

    getFilesystem:function (success, fail) { 
     window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail); 
    }, 

    getFolder: function (fileSystem, folderName, success, fail) { 
     fileSystem.root.getDirectory(folderName, { create: true, exclusive: false }, success, fail) 
    }, 

    transferFile: function (uri, filePath, progress, success, fail) { 
     var that = this; 
     that.progress = progress; 
     that.success = success; 
     that.fail = fail; 

     var transfer = new FileTransfer(); 
     transfer.onprogress = function (progressEvent) { 
      if (progressEvent.lengthComputable) { 
       var perc = Math.floor(progressEvent.loaded/progressEvent.total * 100); 
       typeof that.progress === 'function' && that.progress(perc); // progression on scale 0..100 (percentage) as number 
      } else { 
      } 
     }; 

     transfer.download(
       uri, 
       filePath, 
       function (entry) { 
        console.log("File saved to: " + entry.toURL()); 
        typeof that.success === 'function' && that.success(entry); 
       }, 
       function (error) { 
        console.log("An error has occurred: Code = " + error.code); 
        console.log("download error source " + error.source); 
        console.log("download error target " + error.target); 
        console.log("download error code " + error.code); 
        typeof that.fail === 'function' && that.fail(error); 
       } 
     ); 
    }, 

    unzip: function (folderName, fileName, success, fail) { 
     var that = this; 
     that.success = success; 
     that.fail = fail; 

     zip.unzip("cdvfile://localhost/persistent/" + folderName + "/" + fileName, 
        "cdvfile://localhost/persistent/" + folderName, 
       function (code) { 
        console.log("result: " + code); 
        that.getFilesystem(
          function (fileSystem) { 
           console.log("gotFS"); 
           that.getFolder(fileSystem, folderName + "/ftpack", function (folder) { 
            document.getElementById("imgPlace").src = folder.nativeURL + "/img.jpg"; 
            folder.getFile("text.html", { create: false }, function (fileEntry) { 
             fileEntry.file(function (file) { 
              var reader = new FileReader(); 
              reader.onloadend = function (evt) { 
               console.log("Read as text"); 
               console.log(evt.target.result); 
               document.getElementById("txtPlace").innerHTML = evt.target.result; 
               typeof that.success === ' function && that.success();' 
              }; 
              reader.readAsText(file); 
             }, function (error) { 
              console.log("Failed to get file"); 
              typeof that.fail === 'function' && that.fail(error); 
             }); 
            }, function (error) { 
             console.log("failed to get file: " + error.code); 
             typeof that.fail === 'function' && that.fail(error); 
            }); 
           }, function (error) { 
            console.log("failed to get folder: " + error.code); 
            typeof that.fail === 'function' && that.fail(error); 
           }); 
          }, function (error) { 
           console.log("failed to get filesystem: " + error.code); 
           typeof that.fail === 'function' && that.fail(error); 
          }); 
       } 
     ); 
    } 
} 
</script> 

因为我想实现这样的家伙在他的职位,下载一个ZIP文件,然后解压到本地沙盒文件系统。当我使用“多设备混合应用程序”+ Cordova扩展在Visual Studio 2013中复制并粘贴此代码并尝试使用Ripple-Nexus7(平板电脑)运行它时,它带来了一个不错的用户界面,但是当我单击“加载”第一次在下面的一段代码中给我错误。

 getFilesystem:function (success, fail) { 
     window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail); 
    }, 

“LocalFileSystem”没有定义,那么我想补充文件,从内VS2013 config.xml中的插件选项卡中文件传输的插件,但它无法取得使用Plugman这些插件。最后,我从GitHub下载了这些软件包,并在解决方案资源管理器中的项目中添加了一个plug_ins文件夹,并将这些插件放入该plug_ins文件夹中,保持其内部文件和文件夹结构不变。现在它不再给我这个错误了(尽管我还没有能够在intellisense中找到LocalFileSystem对象,这意味着一些奇怪的原因,它不会给我同样的LocalFileSystem未定义的错误,但仍然这些插件不工作)。

现在,当我运行代码和UI出现后,我点击“加载”按钮什么也没有发生,在此之前,“OnDeviceReady”函数应该在“deviceready”事件中调用,但没有任何事情发生在那里。

请大家在这方面我需要很多帮助,因为我对JavaScript和科尔多瓦的东西很陌生。 我会衷心感谢所有能够帮助我解决这些问题的人。

+0

我把我的笔记本电脑带到不同的位置,我可以通过使用config.xml的插件选项卡下载Cordova扩展,并构建成功。但它仍然给我同样的错误LocalFileSystem未定义。现在我完全掌握了什么是错的想法。 – user2913184 2014-10-30 01:20:34

+0

我真的很感激任何意见/建议。 – user2913184 2014-10-31 16:30:50

+0

你有没有试过用仿真器代替纹波运行你的代码? Chrome浏览器默认不启用文件系统访问权限。要解决这个问题,请按照第一条评论[这里]中的步骤操作(http://www.tricedesigns.com/2012/07/31/emulating-phonegapcordova-apps-in-the-browser/) – Avani 2014-10-31 18:18:04

回答

0

是的Avani我已经知道涟漪是罪魁祸首,对不起,我没有更新,应该有。它让所有的插件在运行时加载,但不让它们初始化,这就是为什么我遇到了所有未定义对象的问题。而且它是Visual Studio 2013中的一个测试版本,所以涟漪与它有关联。是的,模拟器给我同样的痛苦。