简体   繁体   中英

Node js - How to send request into small chunk of data (Error: request entity too large)

Every time I am trying to send a huge Json object(contains images base64) over the network to my node js server.I get this

Error: request entity too large

There is solution like this link ( stackoverflow question ) that recommend to increase the limit on the node js server. I am looking for a client side solution. Is there any way to break down the data sent and have the server wait to receive all the packets and then process? (promise??)

Hypothetically speaking , if my files is 20mb, even if my limit is set to 5mb, I will be able to send it, it will just take more time. Is it possible? Can you please put me on the right track?

I am using express on the back end side

Here some code to refer:

    //AJAX CALL
    var promise = $.Deferred();
    $.ajax('/module/report/ddl',{
        data: {
            widget: JSON.stringify(widget), <--- large, very large
            filename: filename,
            username: username,
            reportname: reportName
        },
        type: 'POST',
        success: function(result){
            //Do something
            promise.resolve(result);
        },
        error :function(err) {
            /* Act on the event */
            promise.reject(err);
        }
    });
    return promise;
}

Here is my server side

   router.route('/ddl').post(ddlReport));
   function ddlReport(req, res){
     var filename = req.body.filename;
     var widgets = req.body.widget; <--- Once again large
     var username = req.body.username;


ctrl.ddlProcess(widgets,filename,title,username,function(err, result){
    if(err){
        res.status(422).send(err);
    }else {
        res.send(result);
    }
});

}

If you need anymore information, let me know! I am trying to find a solution that would be best even if it is not the one that I suggested!

Thank you for your time! Have good day!

Have you tried to break your large json into small pieces and send them separately? Just add some additional info like {packet: 1, total: 20, data: ... (one small piece of large data)} Then send them at same time via ajax or one by one. And on node server side collect all of those 20 pieces. Additionally you can have some checksum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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