Javascript 使用 Phonegap 访问文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10461680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:24:55  来源:igfitidea点击:

Access files using using Phonegap

javascriptiphoneios5cordova

提问by Sana Joseph

I'm trying to work with files on IOS, using Phonegap[cordova 1.7.0]. I read how to access files and read them on the API Documentationof phone gap. But I don't know, when the file is read where will it be written ? & How can I output the text, image, or whatever the text is containing on the iPhone screen?

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

Here's the code I'm using:

这是我正在使用的代码:

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

采纳答案by Sana Joseph

That's what worked for me in case anyone needs it:

这对我有用,以防万一有人需要它:

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

回答by sherb

As of Cordova 3.5 (at least), FileReaderobjects only accept Fileobjects, not FileEntryobjects (I'm not sure about prior releases).

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

Here's an example that will output the contents a local file readme.txtto the console. The difference here from Sana's example is the call to FileEntry.file(...). This will provide the Fileobject needed for the call to the FileReader.readAsfunctions.

这是一个将本地文件内容输出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);});
}

回答by Mayur Birari

If you are using phonegap(Cordova) 1.7.0, following page will work in iOS, replace following template in index.html

如果您使用的是 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>