【发布时间】:2019-03-10 09:51:12
【问题描述】:
我正在尝试从发送到 SailsJs 服务器的 blob 中获取缓冲区。
发送到服务器的示例如下:
Blob(3355) {size: 3355, type: "video/x-matroska;codecs=avc1,opus"}
在服务器端,我执行以下操作:
let body = new Array(0);
let buffer;
let readable = req.file('recordingPart');
readable.on('data', (chunk) => {
body.push(new Buffer(chunk));
});
readable.on('end', () => {
buffer = Buffer.concat(body);
console.log('There will be no more data.', buffer.length, buffer);
});
运行这部分代码时出现错误:
buffer.js:226
throw new errors.TypeError(
^
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, buffer, arrayBuffer, array, or array-like object. Received type object
at Function.from (buffer.js:226:9)
at new Buffer (buffer.js:174:17)
...
在这种情况下,错误位于new Buffer(chunk) 上的body.push(new Buffer(chunk));
我的第一个方法是类似的:
let body = [];
let buffer;
let readable = req.file('recordingPart');
readable.on('data', (chunk) => {
body.push(chunk);
});
readable.on('end', () => {
buffer = Buffer.concat(body);
console.log('There will be no more data.', buffer.length, buffer);
});
但我遇到了这个错误:
buffer.js:475
throw kConcatErr;
^
TypeError [ERR_INVALID_ARG_TYPE]: The "list" argument must be one of type array, buffer, or uint8Array
at buffer.js:450:20
在这个错误中弹出Buffer.concat(body);
我从这个答案Node.js: How to read a stream into a buffer?得到了一些指导
谁能帮我从req.file获取buffer。
【问题讨论】:
标签: javascript node.js stream sails.js buffer