【问题标题】:Phonegap,ios filereader doesn't workPhonegap,ios文件阅读器不起作用
【发布时间】:2013-03-14 12:09:58
【问题描述】:
var xml2;

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

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

function gotFS(fileSystem){
    fileSystem.root.getFile("ccw.xml", null, gotFile, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.file(gotFile, fail);
}

function gotFile(file) {
    readAsText(file);
}

function readAsText(file) {

    var reader = new FileReader();

    reader.readAsText(file);    

    reader.onload = function(){
    };

    xml2 = reader.result;

    writeJson();

}

function fail(evt) {
    console.log(evt.target.error.code);
}

function parseXml(xml) {

    var dom = null;

    if (window.DOMParser) {
        try {
            dom = (new DOMParser()).parseFromString(xml, "text/xml");
        }
        catch (e) { dom = null; }
    }
    else if (window.ActiveXObject) {
        try {
            dom = new ActiveXObject('Microsoft.XMLDOM');

            dom.async = false;

            if (!dom.loadXML(xml)) // parse error ..
          window.alert(dom.parseError.reason + dom.parseError.srcText);
        }
        catch (e) { dom = null; }
    }
    else
       alert("oops");

   return dom;
}

function writeJson(){

    var json;

    //    for(var i = 0 ; i < xml.length ; i++) {
    show("\n\n" + (json = xml2json(parseXml(xml2), "  ")) + "\n\n");
    //        console.log("====="+i+"/"+xml.lenght+"=====");
    //    }
}

function show(s) { 

    document.getElementById("out").innerHTML += (s+"\n").replace(/&/g, "&amp;").replace (/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g, "<br/>") + "<hr/>";
}

这段代码是xml到json的解析代码。我遵循phonegap参考。但我的文件阅读器不会改变 stateReady。那总是状态0.请帮助我.. filereader.result 方法在 phonegap 中不起作用? 我想回家。 :-( .i 将永远看到这篇文章。

【问题讨论】:

  • 应用是否调用了 OnDeviceReady?您可以通过在 OnDeviceReady 事件中放置一个简单的警报来测试它。

标签: ios html cordova filereader


【解决方案1】:

好吧,你可能已经解决了,但如果对其他人有用,你的回调没有正确链接,只需用这一行更改你的行

fileSystem.root.getFile("ccw.xml", null, gotFile, fail);

fileSystem.root.getFile("ccw.xml", null, gotFileEntry, fail`);

尝试这样做,也有助于添加控制台日志消息,以便您更轻松地调试和了解发生的情况以及发生的顺序。

【讨论】:

    猜你喜欢
    • 2016-09-05
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多