OGeek|极客世界-中国程序员成长平台

标题: javascript - 在 Cordova iOS 中使用 Papaparse 解析本地 CSV 文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:08
标题: javascript - 在 Cordova iOS 中使用 Papaparse 解析本地 CSV 文件

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

提前致谢!



Best Answer-推荐答案


同样的问题。

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

... for some reason, iOS is returning 0 as the status for the xhr request even when there is data loaded from file

所以我在 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 中添加了相同的答案. 可能会包含这个补丁。

关于javascript - 在 Cordova iOS 中使用 Papaparse 解析本地 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38194078/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4