0

我有来自此链接的以下代码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 文件,然后将其解压缩到本地沙盒文件系统。当我使用“多设备混合应用程序”+ Cordova 扩展在 Visual Studio 2013 中复制并粘贴此代码并尝试使用 Ripple-Nexus7(平板电脑)运行它时,它会显示一个不错的 UI,但是当我单击“加载”按钮时这是第一次在下面的代码中给我错误。

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

未定义“LocalFileSystem”然后我尝试从 VS2013 中的 config.xml 插件选项卡添加文件、文件传输插件,但使用 Plugman 无法获取这些插件。最终,我从 GitHub 下载了这些包,并从解决方案资源管理器在我的项目中添加了一个插件文件夹,并将这些插件放入该插件文件夹中,保持其内部文件和文件夹结构完好无损。现在它不再给我那个错误了(尽管我仍然没有能够在智能感知中找到 LocalFileSystem 对象,这意味着由于某种奇怪的原因,它没有返回相同的 LocalFileSystem 未定义错误,但这些插件仍然无法正常工作)。

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

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

4

1 回答 1

0

是的,Avani 我已经发现涟漪是罪魁祸首,抱歉我没有更新,但应该有。它让所有插件在运行时加载,但不让它们初始化,这就是为什么我遇到所有未定义对象的问题。它是 Visual Studio 2013 中的测试版,因此涟漪有与之相关的问题。是的,模拟器给了我同样的痛苦。

于 2014-10-31T21:54:49.727 回答