简体   繁体   English

如何在Node.js / Express中发布大量JSON数据(而非文件)

[英]How to post large amount of json data (not file) in nodejs/express

So I am doing an ajax(jquery) post that uploads quite a big amount of json data. 所以我正在做一个ajax(jquery)帖子,它上传了大量的json数据。 When posting a large data, the data is generally broken into chunks. 当发布大型数据时,数据通常会分成多个块。 So we have to listen for post data requests and construct a full buffer of the upload data. 因此,我们必须侦听发布数据的请求并构造上载数据的完整缓冲区。 Something like this: 像这样:

    req.on('data', function(chunk) {
        console.log("upload on data "+ chunk.length);
        chunks.push(chunk);
        total+= chunk.length;
    });
    req.on('error', function(e) {
            console.log('Got Error ' + e.message);
    });
    req.on('end', function() {
        var buf = new Buffer(total)
            cur = 0;
        for (var i = 0, l = chunks.length; i < l; i++) {
            chunks[i].copy(buf, cur, 0);
            cur += chunks[i].length;
        }

        var level = 1;
        var path = "level"+level+".json";
        writeToFile(buf,path);
        res.send("Update successfull as level "+level);
    });

This seems to be working if I am uploading a file with form: 如果我要上传具有以下格式的文件,这似乎可以正常工作:

function display_form(req, res) {
    res.sendHeader(200, {"Content-Type": "text/html"});
    res.write(
        '<form action="/upload" method="post" enctype="multipart/form-data">'+
        '<input type="file" name="upload-file">'+
        '<input type="submit" value="Upload">'+
        '</form>'
    );
    res.close();
}

But I need to upload json data (which is dynamic). 但是我需要上传json数据(是动态的)。 I am doing this in this way: 我这样做是这样的:

           $.ajax({
                type: "POST",
                url: "/upload",
                data: {"data": JSON.stringify(gamePack)},
                success: cb,
            });

Then there seems to be no callback to req 'data' or 'end'. 然后似乎没有回调来请求“数据”或“结束”。 So how are uploading files different from posting data? 那么上传文件与发布数据有何不同?

This seems to have solved my problem: 这似乎已经解决了我的问题:

$.ajax({
        type: "POST",
        dataType: 'json',
        url: "/upload",
        contentType:"application/jsonrequest",
        data: JSON.stringify(gamePack),
        success: cb,
});

POST 'ing data via an XMLHttpRequest (which is what jQuery uses from within) isn't streaming. POST “经由荷兰国际集团的数据XMLHttpRequest (这是从jQuery的内使用)不流。 The whole item will be posted to the server and then the handler will fire, with the content already loaded (in req.body ). 整个项目将被发布到服务器,然后处理程序将触发,其中内容已加载(在req.body )。 If you want this to be sent chunked, then you will have to implement this yourself. 如果要分块发送,则必须自己实现。 For instance, by using the HTML5 FileSystem API to read files, chunk them yourself, and use a websocket to write the chunked data to the server. 例如,通过使用HTML5 FileSystem API读取文件,自己对文件进行分块,然后使用websocket将分块的数据写入服务器。 I have done something like this in socketio-upload , so you might want to look into that. 我在socketio-upload中做了类似的事情,所以您可能需要研究一下。

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

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