首页 > 解决方案 > 如何通过 ajax 将数百万行从 DB 导出到 CSV?

问题描述

我的数据库中有数百万行。我需要将它们全部导出到 csv 文件中。问题是我有超时限制错误。

所以。我决定通过ajax来做。

我创建了生成几个 tmp csv 文件的 ajax

function chunkedExport()
{
    $.ajax({
        type: "POST",
        url: "transactions/",
        dataType: "JSON",
        data: $("#transactions").serialize(),
        async: false,
        success: function(response)
        {
            if (response.status === "continue")
            {
                $("#start_from").val(response.start_from);
                $("#client_file_name").val(response.client_file_name);
                $("#file_name").val(response.tmp_file_name);
                $("#step").val(response.step);
                chunkedExport();
            } else {
                console.log('DOWNLOAD');
                $("#start_from").val(0);
                $("#client_file_name").val('');
                $("#file_name").val('');
                $("#step").val(1);
                $("#export_type").val("");
                $('#csv_format').val("");

                document.location.href = '/transactions_export_csv/'+response.client_file_name+'/'+response.tmp_file_name+'/'+response.total_iterations+'/'
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log([xhr, ajaxOptions, thrownError]);
            $("#start_from").val(0);
            $("#client_file_name").val('');
            $("#file_name").val('');
            $("#step").val(1);
            $("#export_type").val("");
            $('#csv_format').val("");
        }
    });
}

问题是如何将它们全部合并到一个 csv 文件中并下载?

标签: phpmysqlajaxcsv

解决方案


推荐阅读