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.