点击“;加载“;按钮未触发事件,并且未定义LocalFileSystem

Click on "Load" Button is not firing the event and also LocalFileSystem is not defined

本文关键字:LocalFileSystem 事件 未定义 加载 按钮 点击      更新时间:2023-09-26

我有以下来自此链接的代码http://weblogs.asp.net/soever/cordova-file-transfer-unzip-and-present-adventures根据我的需要只更改了URL。

<!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文件,然后将其解压缩到本地沙盒文件系统。当我使用"Multi-Device Hybrid App"+Cordova扩展在visual studio 2013中复制和粘贴此代码,并尝试使用Ripple-Nexos7(平板电脑)运行它时,它显示了一个不错的UI,但当我第一次点击"加载"按钮时,它在下面的代码中给了我错误。

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

"LocalFileSystem"没有定义,然后我试图从VS2013中config.xml的插件选项卡中添加File、FileTransfer插件,但使用Plugman无法获取这些插件。最终,我从GitHub下载了这些包,并从解决方案资源管理器在我的项目中添加了一个plug_ins文件夹,并将这些插件放入该plug_ins文件夹中,保持其内部文件和文件夹结构不变。现在它不再给我那个错误了(尽管我仍然无法在intellisense中找到LocalFileSystem对象,这意味着出于某种奇怪的原因,它不会给我同样的LocalFileSystem未定义错误,但这些插件仍然不工作)。

现在,当我运行代码时,一旦我单击"加载"按钮,UI就会出现,什么都没有发生——绝对没有发生——在此之前,"OnDeviceReady"函数应该在"deviceready"事件中调用,但那里也没有发生任何事。

伙计们,我在这方面需要很多帮助,因为我对Javascript和Cordova的东西很陌生。我将衷心感谢所有能帮助我解决这些问题的人。

是的,Avani我已经发现ripple是罪魁祸首,很抱歉我没有更新,但应该更新。它允许所有插件在运行时加载,但不允许它们初始化,这就是为什么我对所有未定义的对象都有问题。它是Visual Studio 2013的测试版,所以ripple也有相关问题。是的,模拟器也给我带来了同样的痛苦。