0

我正在尝试从 index.html 访问我的一个 phonegap 应用程序的 www 文件夹内的文本文件。我不认为 filereader 工作,因为它访问手机的文件系统,而不是应用程序的文件系统。无论如何,这是我尝试过的,当在 gotFS 中调用此行时,它会跳转到错误函数:

     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>
4

1 回答 1

0
$.get("version.txt", function(data) {
   // there u have version.txt data (in var data)
});
于 2013-06-19T16:10:17.673 回答