使用Phonegap访问文件

Access files using using Phonegap

本文关键字:文件 访问 Phonegap 使用      更新时间:2023-09-26

我正在尝试使用 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);
}

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

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

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

如果有人需要它,这就是对我有用的方法:

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

如果您使用的是phonegap(Cordova)1.7.0,则以下页面将在iOS中工作,请替换索引中的以下模板.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>