客户代码:
var xhr = new XMLHttpRequest();
xhr.open('POST', '/frame', true);
xhr.send(blob);
服务器代码:
app.use(bodyParser.urlencoded({extended: false,limit: '50mb'}));
app.post('/frame', function (req, resp) {
console.log(req.body);
});
这给了PayloadTooLargeError:参数太多了 添加
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
无法解决问题。还有其他想法吗?
答案 0 :(得分:6)
假设您的blob变量不是真正的url编码形式数据和任何类型的内容。然后在服务器端,您可以在它进入时读取请求流。请记住,req
事件处理程序上的http.Server.request
变量是可读流。这将删除body-parser
中间件强加的任何大小限制。保留原始客户端代码,然后是服务器代码:
// app.use(bodyParser.urlencoded({extended: false,limit: '50mb'}));
app.post('/frame', function (req, resp) {
req.on('readable', function(){
console.log(req.read());
});
});
如果内容太大,即使对于结构化数据,流处理的处理请求也是一个好主意。例如,在我使用带有大json请求的body-parser#json
中间件并解决删除body-parser#json
中间件并使用oboe来解析流式输入的问题时,我遇到了性能问题。
答案 1 :(得分:1)
您的blob
变量超出了服务器中设置的限制大小。您必须设置一个始终大于客户端数据的数字(blob
)。
客户:
var xhr = new XMLHttpRequest();
xhr.open('POST', '/frame', true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(blob);
服务器:
// set limit with a value always bigger than the client data
var upperBound = '1gb';
app.use(bodyParser.urlencoded({extended: false, limit: upperBound}));
app.post('/frame', function (req, resp) {
console.log(req.body);
});