0

I have following code from this link http://weblogs.asp.net/soever/cordova-file-transfer-unzip-and-present-adventures changed just the URL according to my needs.

<!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>

Since I'm trying to achieve something like this guy in his post that download a zip file and then unzip it to local sandboxed file system. When I copied and pasted this code inside visual studio 2013 using "Multi Device Hybrid App" + Cordova extension and tried running it in using Ripple-Nexus7 (tablet) it brings up a nice UI but when I clicked on "Load" button for the very first time it was giving me error in the following piece of code.

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

"LocalFileSystem" is not defined then I tried to add File, FileTransfer plugins from config.xml's plug-in tab inside VS2013 but it failed to fetch these plugins using Plugman. Eventually I downloaded these packages from GitHub and added a plug_ins folder inside my project from solution explorer and put these plugins into that plug_ins folder keeping their internal file and folder structure intact.Now its not giving me that error anymore (though I'm still not able to find LocalFileSystem object in intellisense which means for some weird reason it's not giving me same LocalFileSystem undefined error back but still these Plugins are not working).

Now when I run the code and UI comes up once I click "Load" button nothing happens absolutely nothing and before that "OnDeviceReady" function should get invoked at "deviceready" event but nothing is happening there as well.

Please guys I need great deal of help in this regard as I'm very new to Javascript and Cordova stuff. I will do a heartfelt thanks to all those who can help me in getting these issues straighten out.

Priyank
  • 1,568
  • 10
  • 15
user2913184
  • 590
  • 10
  • 33
  • I took my laptop to a different location and I was able to download Cordova extensions by using config.xml's plug-in tab and built it the build succeeded. But still it gives me same error of LocalFileSystem undefined. Now I'm totally running of idea as to what is wrong. – user2913184 Oct 30 '14 at 01:20
  • I will really really appreciate any comments/suggestions. – user2913184 Oct 31 '14 at 16:30
  • Have you tried running your code with the emulator instead of ripple? Filesystem access is not enabled by default in Chrome. To fix it, follow the steps in the first comment [here](http://www.tricedesigns.com/2012/07/31/emulating-phonegapcordova-apps-in-the-browser/) – Avani Oct 31 '14 at 18:18
  • Wonderful. Could you please update the question with your answer so the community can make itself aware? Thanks! – Avani Oct 31 '14 at 21:47
  • I have posted an answer with my earlier comments being fed into it. – user2913184 Oct 31 '14 at 21:55
  • Going to try it on Android Device, lets see what it gives me, will update all of you. – user2913184 Oct 31 '14 at 21:57
  • Tried Nexus 10 with Visual Studio, I was getting deployment error. I went ahead and updated drivers on my machine and then installed the corresponding Android API 20 using Android SDK Manager but still the same old deployment error. I would rather guess that Multi Device Hybrid stuff inside VS2013 (as I mentioned earlier is in Beta version) so its pretty immature and putting high hopes on it for the time being is not an advisable thing. Though I love Visual Studio for whatever I got from it in the past. And choosing Eclipse over VS is something I would hate but have no other way. – user2913184 Nov 04 '14 at 17:09

1 Answers1

0

Yes Avani I have already figured it out that ripple is the culprit, sorry I didn't update, should have though. Its letting all the plugins load during the run-time but not letting them initialize and that's why I'm having problem with all the objects being undefined.And it's a beta version in Visual Studio 2013 so ripple has issues associated with it. Yes the emulator is giving me the same pain.

user2913184
  • 590
  • 10
  • 33