0
我想從index.html中訪問我的phonegap應用程序之一的www文件夾內的文本文件。我不認爲filereader工作,因爲它訪問手機的文件系統,而不是應用程序的文件系統。無論如何,這裏是我的嘗試,並跳轉到時這條線gotFS內稱爲誤差函數:有沒有辦法使用phonegap從同一個www文件夾內的另一個文件訪問www文件夾內的文件?
fileSystem.root.getFile("version.txt", null, gotFileEntry, fail);
這裏是我的全部的index.html代碼:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<script charset="utf-8" src = "jquery-1.10.1.min.js"></script>
<script charset="utf-8" src = "cordova-2.7.0.js"></script>
<script>
function test()
{
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
document.write("gotFSreached");
fileSystem.root.getFile("version.txt", null, gotFileEntry, fail); // this jumps to fail
}
function gotFileEntry(fileEntry) {
document.write("gotFileEntryreached");
fileEntry.file(gotFile, fail);
}
function gotFile(file){
document.write("gotFilereached");
readAsText(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as text");
document.write(evt.target.result);
};
reader.readAsText(file);
}
function fail(evt) {
document.write("error");
}
</script>
<body>
<button type="button" onclick="test()">Print version.txt</button>
</body>
</html>
似乎不正在工作。我只是把這個代碼放在按鈕點擊調用的函數中,然後在函數(數據)內我試圖做一個document.write(data);但沒有任何反應。 – user2463517
uhm真的嗎?,這對我來說很完美:檢查控制檯日誌頁,我的猜測是,現在你得到了一些交叉源錯誤 –