获取excel数据并使用ajax发布到服务器端

问题描述 投票:0回答:1

我正在尝试使用js和ajax从excel收集数据

    var ExcelToJSON = function () {
    this.parseExcel = function (file) {
        var reader = new FileReader();
        reader.onload = function (e) {
            var data = e.target.result;
            var workbook = XLSX.read(data, {
                type: 'binary'
            });
            var dataArray = [];
            workbook.SheetNames.forEach(function (sheetName) {
                var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
                var json_object = JSON.stringify(XL_row_object);
                dataArray.push(json_object);
                //jQuery('#xlx_json').val(json_object);

                $.ajax({
                    url: "test2.aspx/InsertExcelGeo",
                    type: "post",
                    data: JSON.stringify({ dataArray: dataArray }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (_data) {
                        alert(_data.d);
                    }
                })
            })
        };
        reader.onerror = function (ex) {
            console.log(ex);
        };
        reader.readAsBinaryString(file);
    };
};

此代码功能齐全,上传的文件是一个3张的excel文件,列内容相同但数据不同,所有数据都可以收集到服务器端

但这3将同时运行,我想先将数据收集到数组中,然后将其发布到ajax,但是当我将ajax post放在外面循环时,ajax post将不起作用

我想首先使用循环收集所有数据并将其粘贴到数组上,然后在循环之后我希望ajax post使用收集的数组数据运行

javascript ajax javascript-objects asp.net-ajax
1个回答
0
投票

使用 Promise 来管理异步。

您可以创建函数来封装您的ajax调用并返回promise。

function ajaxCall(dataArray) => {
    return new Promise ((resolve, reject)=>{$.ajax({
      url: "test2.aspx/InsertExcelGeo",
      type: "post",
      data: JSON.stringify({ dataArray: dataArray }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (_data) { resolve(_data.d) },
      error: function () {reject()}
      });
    })
}

然后你可以调用这个返回 Promise 的函数,并等待所有 ajax 调用完成来管理答案:

var ExcelToJSON = async function () { //Your function must become asynchronous
    this.parseExcel = function (file) {
        const promises = []; //You will need an array to store the promises
        var reader = new FileReader();
        reader.onload = function (e) {
            var data = e.target.result;
            var workbook = XLSX.read(data, {
                type: 'binary'
            });
            var dataArray = [];
            workbook.SheetNames.forEach(function (sheetName) {
                var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
                var json_object = JSON.stringify(XL_row_object);
                dataArray.push(json_object);
                //jQuery('#xlx_json').val(json_object);

                promises.push(ajaxCall(dataArray)); //Store all the promises within your for loop
            });
            //wait for all the promises to finish
            const results = await Promise.allSettled(promises);
            results.forEach((result) => {
                if(result.status === 'fulfilled'){
                    //Code when response was 200
                    console.log(result.value.data)
                } else {
                    //Code when response was other than 200
                }
            })
        };
        reader.onerror = function (ex) {
            console.log(ex);
        };
        reader.readAsBinaryString(file);
    };

这应该可以解决问题。 祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.