简体   繁体   English

将大型JavaScript数组发送到Web API

[英]Sending large javascript array to Web API

I am struggling with this issue for 2 days... 我在这个问题上苦苦挣扎了2天...

I have a JavaScript array (20,000K rows and 41 columns). 我有一个JavaScript数组(20,000K行和41列)。 It was originally received in javaScript through an ajax call as shown below, 它最初是通过ajax调用在javaScript中接收的,如下所示,

var dataArray = [];
var dataRequest = {};
            dataRequest.SearchCondition = 'some value'; 
            $.ajax({
                type: "POST",
                url: "api/GetData/ProcessRequest",              
                dataType: 'json',
                cache: false,
                async: true,
                crossDomain: false,
                data: dataRequest ,
                success: function (response) {
                  dataArray = response; 
                },
                error: function (xhr, textStatus, errorThrown) {
                   dataArray = null;
                }
            });

In the application, the user will verify the data and send it back to Web API method. 在应用程序中,用户将验证数据并将其发送回Web API方法。

I am trying to send the same data back (dataArray) to web api method but, it fails. 我试图将相同的数据(dataArray)发送回Web api方法,但是失败。 Please see the code below, 请查看下面的代码,

Option 1: (failed - the request did not hit web api method) 选项1 :(失败-请求未命中Web API方法)

 var dataArrayJsonStr = JSON.stringify(dataArray);                     

    $.ajax({
        type: "POST",
        url: "api/SendData/ProcessRequest",
        dataType: 'json',
        data: {'dataValue':dataArrayJsonStr },
        success: function (response) {
            alert('success');
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown)
        }
    });

In IE 8, I am getting 'out of memory' exception popup. 在IE 8中,我收到“内存不足”异常弹出窗口。 (most of our application users still have IE 8) (我们的大多数应用程序用户仍然使用IE 8)

In Chrome, it crashes. 在Chrome浏览器中,它崩溃了。

Option 2 tried: (don't know how to read the value) 选项2尝试过:(不知道如何读取值)

I tried to send the same value to web api through XmllHttpRequest 我试图通过XmllHttpRequest将相同的值发送到Web api

var dataArrayJsonStr = JSON.stringify(dataArr);
            var xmlRequest;
            if (window.XMLHttpRequest) {
                xmlRequest = new XMLHttpRequest();                
            }
            xmlRequest.open("POST", "api/SendData/ProcessRequest", false);
            xmlRequest.setRequestHeader('Content-Type', 'application/text');
            xmlRequest.send("dataValue=" + dataArrayJsonStr);

Using Chrome, I am able to post the data successfully to Web API, I am seeing the content-length as '128180309'. 使用Chrome,我可以将数据成功发布到Web API,并且看到内容长度为“ 128180309”。 But, I don't see the values. 但是,我看不到这些值。 How do i get the values in Web API? 如何获取Web API中的值?

Please suggest me how to send large data back to web api from javascript. 请建议我如何将大数据从JavaScript发送回Web API。

Thanks, Vim 谢谢,Vim

I think you create overhead, maybe I wrong, you can edit me. 我认为您造成了开销,也许我错了,您可以编辑我。 Did you really need send back all datas back or you just need send modified data? 您是否真的需要发回所有数据,或者只需要发送修改过的数据? Because in real life hard to imagine that user will review 20.000 of rows. 因为在现实生活中很难想象用户会查看20.000行。

Good example is ExtJS stores, you can see example here Key thing of stores that they send back to the server only modified or deleted data, it save browser, network and server resources. 一个很好的例子是ExtJS存储,您可以在此处看到示例,它们只将修改或删除的数据发送回服务器的存储的关键之处,它节省了浏览器,网络和服务器资源。

Try to add more memory for API or more time excecution, also you can try return data in more small parts. 尝试为API或执行更多时间而增加内存,也可以尝试在更小的部分中返回数据。 Defining the number of parts to send. 定义要发送的零件数量。

Did you try to send the data by chunks? 您是否尝试按块发送数据? I mean, you need to split it in small pieces and perform multiple number of requests. 我的意思是,您需要将其拆分成小块并执行多个请求。

For example, it can be like: 例如,它可能像:

--HELLO SERVER. STARTING TRANSMITION FOR DATA SET #177151--  
PIECE 1/13  
PIECE 2/13  
...  
PIECE 13/13  
--BUE SERVER-- 

So, it will take some time, but you can send any amounts of data without memory problems. 因此,这将需要一些时间,但是您可以发送任何数量的数据而不会出现内存问题。 If you're struggling with it for 2 days, I think you got some time to code it :) 如果您在2天的时间里都在苦苦挣扎,我想您有一些时间来编写它了:)

UPD1 : Client code example. UPD1 :客户端代码示例。

Here's an example of client code. 这是客户端代码的示例。 This is a simple chunking algorithm. 这是一个简单的分块算法。 Have to say I didn't test it, because it would take a lot of time to represent your situation. 不得不说我没有测试它,因为这将花费很多时间来代表您的情况。

So, you should read it and get the point. 因此,您应该阅读并明白这一点。 You have a simple function, that takes you whole data set and callbacks for each response (to update your progress bar, eg), for successful finish and for error. 您有一个简单的函数,可以获取整个数据集和每个响应的回调(例如,更新进度条),成功完成和出错的过程。

Hope, it will help you to make some problems. 希望能帮助您解决一些问题。 Also, I can help you to build architecture on the server-side, but I need to know what technologies do you use. 另外,我可以帮助您在服务器端构建体系结构,但是我需要知道您使用什么技术。

function sendData(data, onEach, onFinish, onError) {
    var CHUNK_SIZE = 1000;
    var isFailed = false;
    var chunkNum = 0;
    var chunk, chunkStart, chunkEnd;

    while(data.length + CHUNK_SIZE > chunkNum * CHUNK_SIZE) {
        if(isFailed) {
            return;
        }
        chunkStart = chunkNum * CHUNK_SIZE;
        chunkEnd   = chunkStart + CHUNK_SIZE + 1;

        chunk = {
            num: chunkNum,
            data: data.slice(chunkStart, chunkEnd)
        };

        ajaxCall(chunk);
        chunkNum++;
    }

    function ajaxCall(data) {
        $.ajax({
            type: "POST",
            url: "api/GetData/ProcessRequest",              
            dataType: 'json',
            async: true,
            data: dataRequest ,
            success: function (response) {
                onEach(data, response);
            },
            error: function (xhr, textStatus, errorThrown) {
                isFailed = true;
                onError(arguments);
            }
        });
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM