【问题标题】:Parsing local CSV file using Papaparse in Cordova iOS在 Cordova iOS 中使用 Papaparse 解析本地 CSV 文件
【发布时间】:2016-07-05 01:42:11
【问题描述】:

我正在使用Papaparse,我正在尝试解析保存在www 内的文件夹中的CSV 文件。

它适用于androidbrowser 平台。

但是,当涉及到iOS 时,它会返回错误回调。

当我输出错误时,它返回undefined

我还检查了iOS的文件路径是否正确,文件确实存在。

我已经尝试将文件路径设置为"folder/myfile.csv",但由于它导致错误,我尝试使用文件插件获取其完整路径。

其他人遇到过同样的问题并有解决方法吗?

这是我的代码。

var getOldData = function() {
    var dir = "folder/myfile.csv",
        file = null;

    file = (isiOS) ? cordova.file.applicationDirectory + "www/" + dir : dir;

    Papa.parse(file, {
        download: true,
        error: function(err, file) {
            console.log(">>>> PAPA PARSE ERROR");
            console.log(">>>>" + err); // this returns undefined
            console.log(">>>>" + file); // this returns undefined as well
        },
        complete: function(results) {
            console.log(">>>> PAPA RESULTS");
            console.log(results);
        },
        header: true,
        dynamicTyping: true
    });
};

提前致谢!

【问题讨论】:

    标签: javascript ios cordova csv papaparse


    【解决方案1】:

    同样的问题。

    我在 ios 上的 pouchdb 加载中找到了 this issue。看来

    ...由于某种原因,即使从文件中加载了数据,iOS 也会返回 0 作为 xhr 请求的状态

    所以我在 papaparse _chunkLoaded 函数中改了,这一行

    if (xhr.status < 200 || xhr.status >= 400)
    

    这个:

    if (!((xhr.status >= 200 && xhr.status < 300) || (xhr.status==0 && xhr.responseText.length>0)))
    

    现在它可以工作了。

    我在相应的github issue 中添加了相同的答案。 可能会包含这个补丁。

    【讨论】:

    • 这就像魔术,哈哈我不得不使用 FileReader API 单独解析文件,然后调用 Papaparse,但你的答案要好得多。谢谢!我希望他们尽快包含补丁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2017-10-24
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多