使用Phonegap访问文件 [英] Access files using using Phonegap

查看:84
本文介绍了使用Phonegap访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用IOS上的文件,使用Phonegap [cordova 1.7.0]。
我阅读了如何访问文件并在 API文档手机差距。但是我不知道,当文件被读取在哪里会被写? &

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 Documentation of 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?

这里是我使用的代码:

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


推荐答案

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访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆