javascript - 在 Cordova iOS 中使用 Papaparse 解析本地 CSV 文件

标签 javascript ios cordova csv papaparse

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

它适用于android浏览器平台。

但是,当涉及到 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
    });
};

提前致谢!

最佳答案

同样的问题。

我找到了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/

相关文章:

javascript - 我们在 SharePoint 中使用的 javascript 窗口解决方案的 jquery 弹出替代方案

javascript - 在 Ember.js 中返回数据之前处理数据

javascript - 在焦点上打开 ngx-bootstrap typeahead

ios - 如何在 IOS 7 的 UISearchBar 中更改位置或隐藏放大镜图标?

javascript - Ionic Tabs 应用程序上的传单仅显示第一个图 block

android - Cordova App sqlite 数据库查看器在运行时应用程序测试,如 Web SQL

javascript - 关于正则表达式模式的解释

ios - 忽略以小写字母开头的 swift 字母

ios - Swift 3 遍历嵌套字典?

xcode - 找不到 Cordova/CDVViewController.h 文件