我嘗試讀取一個文件在我的iOS應用(PhoneGap的2.1 + jquerymobile)與PhoneGap的getFile
功能,但我一直有這樣的錯誤:的PhoneGap 2.1的iOS:在應用WWW文件夾讀取文件
Error in error callback: File3 = TypeError: 'undefined' is not an object
我的文件在www/data/datajson.txt
幷包含一些json數據。
我的代碼:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
//get www folder path
var path = window.location.pathname;
path = path.substr(path, path.length - 10);
var pathwww = path;
console.log(pathwww); /* /var/mobile/Applications/{ID}/{APPNAME}.app/www/ */
fileSystem.root.getFile("file//"+pathwww+"data/datajson.txt", null, gotFileEntry, fail);
// result : Error in error callback: File3 = TypeError: 'undefined' is not an object
pathwww.getFile("file///data/datajson.txt", null, gotFileEntry, fail);
// result : Error in success callback: File2 = TypeError: 'undefined' is not a function
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readDataUrl(file);
readAsText(file);
}
function readDataUrl(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as data URL");
console.log(evt.target.result);
};
reader.readAsDataURL(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as text");
console.log(evt.target.result);
};
reader.readAsText(file);
}
function fail(evt) {
console.log(evt.target.error.code);
}
我不明白爲什麼我不能在我的www
文件夾中瀏覽和閱讀我的datajson.txt
文件。
謝謝你的幫助。
您好,感謝您的幫助。我有同樣的錯誤:錯誤回調中的錯誤:File3 = TypeError:'undefined'不是一個對象。 控制檯日誌完整路徑fileSystem.root =/var/mobile/Applications/{ID}/Documents 如果我嘗試「./data/datajson.txt」我想我在/ var/mobile/Applications/{ ID} 在模擬器上,我的應用程序www文件夾在 /var/mobile/Applications/{ID}/{appname.app}/ – user1361189
好吧,等我推薦你另一個解決方案...給我一些秒同伴 – Littm
嘿夥計,我剛剛重新編輯我的帖子。嘗試新的解決方案,並讓我知道如果這項工作 – Littm