简体   繁体   中英

How can I change the limit of max size of package for a POST request?

I am trying to send an array of bytes with a POST request. I am using Node.js and Express.js for the server side. I get error code 413 or the page even freezes ('PayloadTooLargeError: too many parameters').

My variable base64 looks like this:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAYGBgYHBgcICAcKCwoLCg8ODAwODxYQERAREBYiFRkVFRkVIh4kHhweJB42KiYmKjY+NDI0PkxERExfWl98fKcBBgYGBgcGBwgIBwoLCgsKDw4MDA4PFhAREBEQFiIVGRUVGRUiHiQeHB4kHjYqJiYqNj40MjQ+TERETF9aX3x8p//CABEIBLAGQAMBIgACEQEDEQH/x...

My variable bytes looks like this:

Uint8Array(294508) [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 132, 0, 6, 6, 6, 6, 7, 6, 7, 8, 8, 7, 10, 11, 10, 11, 10, 15, 14, 12, 12, 14, 15, 22, 16, 17, 16, 17, 16, 22, 34, 21, 25, 21, 21, 25, 21, 34, 30, 36, 30, 28, 30, 36, 30, 54, 42, 38, 38, 42, 54, 62, 52, 50, 52, 62, 76, 68, 68, 76, 95, 90, 95, 124, 124, 167, 1, 6, 6, 6, 6, 7, 6, 7, 8, 8, 7, …]

I tried to set in app.js the limit with body-parser and below is my POST request:

    var bodyParser = require('body-parser');
    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
    <input onchange="openFile(event)" type="file" id="FileUpload">

    function openFile(event) {

        var input = event.target;
        var reader = new FileReader();
        reader.onload = function() {
            var base64 = reader.result;
            var bytea = base64ToArrayBuffer(base64); //This converts to bytea

            $.post('/user/update', {username: user.firstname, profilePicture: bytea}, function(err) {
                if(err) {
                    alert(err);
                } else {
                    alert("Miracle");
                }
            });
        };

    reader.readAsDataURL(input.files[0]);
}

I expect that the package reaches the server and stores the data.

Play around with the contentLength in your post request. Maybe the default value is small due to which it cannot send the whole data

Instead of sending the array of bytes, I am sending the base64 and this solved the problem. The error was caused from the array. Probably I can solved the problem adding parameterLimit as suggested here Node.js error: too many parameters Error while uploading bulk data

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