简体   繁体   English

如何更改POST请求的最大包大小限制?

[英]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. 我正在尝试通过POST请求发送字节数组。 I am using Node.js and Express.js for the server side. 我在服务器端使用Node.js和Express.js。 I get error code 413 or the page even freezes ('PayloadTooLargeError: too many parameters'). 我收到错误代码413,或者页面甚至冻结(“ PayloadTooLargeError:参数过多”)。

My variable base64 looks like this: 我的变量base64看起来像这样:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAYGBgYHBgcICAcKCwoLCg8ODAwODxYQERAREBYiFRkVFRkVIh4kHhweJB42KiYmKjY+NDI0PkxERExfWl98fKcBBgYGBgcGBwgIBwoLCgsKDw4MDA4PFhAREBEQFiIVGRUVGRUiHiQeHB4kHjYqJiYqNj40MjQ+TERETF9aX3x8p//CABEIBLAGQAMBIgACEQEDEQH/x... 数据:图像/ 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, …] 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,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,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: 我试图在app.js中设置body-parser的限制,以下是我的POST请求:

    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. 在您的帖子请求中试用contentLength。 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. 我没有发送字节数组,而是发送了base64,这解决了问题。 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 也许我可以解决这里建议的添加parameterLimit的问题Node.js错误:参数太多上传批量数据时出错

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

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