5

我正在尝试使用 Phonegap [cordova 1.7.0] 在 IOS 上处理文件。我阅读了如何访问文件并在电话间隙的API 文档中阅读它们。但我不知道,当文件被读取时,它会被写入哪里?& 我如何在 iPhone 屏幕上输出文本、图像或文本包含的任何内容?

这是我正在使用的代码:

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

function gotFS(fileSystem) {
    fileSystem.root.getFile("readme.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);
}
4

3 回答 3

5

从 Cordova 3.5 开始(至少),FileReader对象只接受File对象,而不是FileEntry对象(我不确定以前的版本)。

这是一个将本地文件内容输出readme.txt到控制台的示例。这里与 Sana 示例的不同之处在于调用FileEntry.file(...). 这将提供File调用函数所需的对象FileReader.readAs

function readFile() {
    window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
        fileSystem.root.getFile('readme.txt', 
            {create: false, exclusive: false}, function(fileEntry) {
                fileEntry.file(function(file) {
                    var reader = new window.FileReader();
                    reader.onloadend = function(evt) {console.log(evt.target.result);};
                    reader.onerror = function(evt) {console.log(evt.target.result);};
                    reader.readAsText(file);
                }, function(e){console.log(e);});
            }, function(e){console.log(e);});
    }, function(e) {console.log(e);});
}
于 2014-06-20T16:55:03.610 回答
4

如果有人需要,这对我有用:

function ReadFile() {
  var onSuccess = function (fileEntry) {
    var reader = new FileReader();
    reader.onloadend = function (evt) {
      console.log("read success");
      console.log(evt.target.result);
      document.getElementById('file_status').innerHTML = evt.target.result;
    };
    reader.onerror = function (evt) {
      console.log("read error");
      console.log(evt.target.result);
      document.getElementById('file_status').innerHTML = "read error: " + evt.target.error;
    };

    reader.readAsText(fileEntry); // Use reader.readAsURL to read it as a link not text.
  };

  console.log("Start getting entry");
  getEntry(true, onSuccess, { create: false });
};
于 2012-05-08T12:32:20.150 回答
3

如果您使用的是 phonegap(Cordova) 1.7.0,以下页面将在 iOS 中工作,替换 index.html 中的以下模板

<!DOCTYPE html>
<html>
  <head>
    <title>FileWriter Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

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

    function gotFS(fileSystem) {
        fileSystem.root.getFile("readme.txt", {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>
  </head>
  <body>
    <h1>Example</h1>
    <p>Write File</p>
  </body>
</html>
于 2012-05-05T12:58:15.980 回答