0
經過數週的研究,我決定使用Phonegap的File API來更新我的應用程序。在PhoneGap的谷歌I組發現,我必須要經過以下步驟:Phonegap File API
- 負載使用XHR
- 該文件將文件寫入到文件系統
- 檢查Internet連接
- 如果是,使用FileTransfer對象下載最新的文件。
- 如果沒有,使用緩存文件
我已經沒有第1步,我的文件,目前正在XHR加載。現在我陷入了第2步。我嘗試了filewriter,但沒有成功,Phonegap的File API上的信息並不十分清晰。誰能幫我這個?
這裏是我的XHR腳本:
<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>
這是我的第一次嘗試在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>
感謝您的提示!其實我死了ip和運行,感謝這個教程:http://www.raymondcamden.com/index.cfm/2012/1/19/Downloading-files-to-a-PhoneGap-application--Part-1 – user2471501
謝謝伊。。上帝是真實的。 – maiko