使用PapaParse下载多个文件? [英] Download multiple files using PapaParse?

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

问题描述

我正在使用 PapaParse 从我的JavaScript脚本下载CSV文件,并且效果很好.

I'm using PapaParse to download CSV files from my JavaScript scripts and it's working great.

但是,我有一个页面需要下载两个文件,然后才能做一些工作,我想知道是否有比这更整洁的方法了:

However, I've got a page where I need to download two files and only then do some work, and I was wondering if there was a neater way to do this than this:

Papa.parse(url_seriesy, {
    download: true,
    header: true,
    keepEmptyRows: false,
    skipEmptyLines: true,
    error: function(err, file, inputElem, reason) { // handle },
    complete: function(y_results) {
            Papa.parse(url_seriesx, {
                download: true,
                header: true,
                keepEmptyRows: false,
                skipEmptyLines: true,
                error: function(err, file, inputElem, reason) { // handle },
                complete: function(x_results) {
                    console.log(x_results.data);
                }
            });
    }
});

这有效,但是非常笨拙.我还能做些什么吗?也许我可以使用诺言?

This works, but is pretty unwieldy. Is there anything else I can do? Perhaps I could use promises?

推荐答案

如果我理解正确,则希望解析每个文件,然后在收集所有结果后执行一些操作.有几种方法可以执行此操作,但这是我可以执行的一种方法(注意:我尚未运行此代码;可能需要进行调整):

If I understand correctly, you want to parse each file and then do something once all the results are collected. There are a few ways to do it but this is one way I might do it (Note: I haven't run this code; it probably needs tweaking):

var files = ["file1.csv", "file2.csv"];
var allResults = [];

for (var i = 0; i < files.length; i++)
{
    Papa.parse(files[i], {
        download: true,
        header: true,
        skipEmptyLines: true,
        error: function(err, file, inputElem, reason) { /* handle*/ },
        complete: function(results) {
            allResults.push(results);
            if (allResults.length == files.length)
            {
                // Do whatever you need to do
            }
        }
    });
}

这篇关于使用PapaParse下载多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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