2012-09-26 70 views
2

我嘗試讀取一個文件在我的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文件。

謝謝你的幫助。

回答

1

假設你的HTML文件是文件夾www中,你可以嘗試以下方法:

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    fileSystem.root.getDirectory("data", {create: true}).getFile("datajson.txt", null, gotFileEntry, fail); 
} 

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); 
} 

有一個嘗試,讓我知道,如果這個工程的伴侶。

+0

您好,感謝您的幫助。我有同樣的錯誤:錯誤回調中的錯誤: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

+0

好吧,等我推薦你另一個解決方案...給我一些秒同伴 – Littm

+0

嘿夥計,我剛剛重新編輯我的帖子。嘗試新的解決方案,並讓我知道如果這項工作 – Littm

2

如果你想從你的應用程序獲得的文本文件,你可以做一個「AJAX」呼籲得到它

 $.ajax({ 
     url : 'data/datajson.txt', 
     type : "get", 
     dataType : "json", 
     success : function(data, textStatus, jqXHR) { 
      console.log('data loaded ' + JSON.stringify(data)); 
     }, 
     error : function(jqXHR, textStatus, errorThrown) { 
      console.log('error loading data :' + errorThrown); 
     } 
    }); 

編輯

如果你想要寫的文件,你無法將它們寫入與您的應用程序相同的位置。你需要把它們寫到你的應用程序的Document文件夾中。這是在安裝時創建的,因此您不能將文件放在XCode中。你將不得不在安裝後複製文件。

你可以訪問你的應用程序的Document文件夾,當你調用window.requestFileSystem

+0

你好,你的解決方案工作正常。 我的目標是閱讀,替換和創建我的應用程序文件夾中的圖像,pdf,json文件。這就是爲什麼我嘗試使用getDirectory和手機庫的getFile – user1361189

+0

你不能替換你的應用程序文件夾中的文件。您只能寫入應用/文檔位置中的文件。 – codemonkey

+0

感謝您的這種精度,我需要更新我的應用程序(www/data /)像圖像或PDF文件,我怎麼能把這些文件放在應用程序/文檔位置?我可以通過xcode訪問這個文件夾來放置所有文件,或者只是在安裝時創建這個文件夾? – user1361189

相關問題