Reputation: 153
After weeks of research I decided to use Phonegap's File API to update my app. On the Phonegap Google Group I discovered that I have to go through following steps:
I already did step 1, my file is now being loaded in XHR. Now I'm stuck at step 2. I tried the filewriter, but without success and the information on Phonegap's File API isn't really clear. Can anyone help me with this?
Here's my XHR script:
<script language="javascript" type="text/javascript">
function loadHome() {
var request = new XMLHttpRequest();
request.open("GET", "file:///sdcard/Download/home.json", true);
request.onreadystatechange = function() {//Call a function when the state changes.
if (request.readyState == 4) {
if (request.status == 200 || request.status == 0) {
var home = JSON.parse(request.responseText);
var data = "<table cellspacing='0'>";
for (i = 0; i < home.length; i++) {
data += "<td>";
data += "<a href='" + home[i].link + "'/>";
data += "<img src='" + home[i].img + "'/>";
data += "<div class='dsc'>" + home[i].expo + "<br><em>";
data += home [i].datum + "</em></div></a></td>";
}
data += "</table>";
var twitter = document.getElementById("home2");
twitter.innerHTML = data;
}
}
}
console.log("asking for home");
request.send();
</script>
And here's my first try at the filewriter:
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("file:///sdcard/Download/home.json", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample text'");
writer.truncate(11);
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample'");
writer.seek(4);
writer.write(" different text");
writer.onwriteend = function(evt){
console.log("contents of file now 'some different text'");
}
};
};
writer.write("some sample text");
}
function fail(error) {
console.log(error.code);
}
</script>
Upvotes: 0
Views: 901
Reputation: 99
I have had lots of problems and frustration with the Phonegap File Api, and so decided to make a simple API to deal with it. My API is posted here:
https://github.com/poja/PhoneGap-FileAPI-Improved
Hope it helps.
Upvotes: 2