[英]Buffer not recognized as buffer on nodejs with sails js
I'm trying to get the buffer from a blob being sent to my SailsJs server. 我正在尝试从blob发送到我的SailsJs服务器的缓冲区。
An example of what is being sent to the server is this: 发送到服务器的示例如下:
Blob(3355) {size: 3355, type: "video/x-matroska;codecs=avc1,opus"} Blob(3355){size:3355,type:“video / x-matroska; codecs = avc1,opus”}
Once on the server side, I do the following: 一旦在服务器端,我执行以下操作:
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);
});
When running this part of the code I get the error: 运行这部分代码时,我收到错误:
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)
...
In this case the error is at the body.push(new Buffer(chunk));
在这种情况下,错误发生在body.push(new Buffer(chunk));
on new Buffer(chunk)
在new Buffer(chunk)
My first approach was similar: 我的第一种方法是类似的:
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);
});
but I've got this error: 但我有这个错误:
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
In this one the error pops at Buffer.concat(body);
在这一个错误弹出Buffer.concat(body);
I got some guidance from this answer Node.js: How to read a stream into a buffer? 我从这个答案得到了一些指导Node.js:如何将流读入缓冲区?
Can anyone help me in getting the buffer
from that req.file
. 任何人都可以帮我从req.file
中获取buffer
。
Your current upload approach will work but there's another new way you might want to consider: 您当前的上传方法将有效,但您可能还需要考虑另一种新方法:
// Upload the image.
var info = await sails.uploadOne(photo, {
maxBytes: 3000000
})
// Note: E_EXCEEDS_UPLOAD_LIMIT is the error code for exceeding
// `maxBytes` for both skipper-disk and skipper-s3.
.intercept('E_EXCEEDS_UPLOAD_LIMIT', 'tooBig')
.intercept((err)=>new Error('The photo upload failed: '+util.inspect(err)));
Also check out the Sails.JS Platzi Course for video tutorials on this latest upload functionality using the example project Ration. 还可以使用示例项目Ration查看Sails.JS Platzi课程,了解有关此最新上载功能的视频教程。
You can get uploadedFile
as below. 您可以按如下方式获取uploadedFile
。
req.file('recordingPart').upload(function (err, uploadedFiles){
if (err) return res.serverError(err);
// Logic with uploadedFiles goes here
});
You can get file descriptor from uploadedFiles[0].fd
and use it to read/stream the file as below. 您可以从uploadedFiles[0].fd
获取文件描述符,并使用它来读取/流式传输文件,如下所示。
fs.readFile(uploadedFiles[0].fd, 'utf8', function (err,data) {
// Logic with data goes here
var myBuffer = Buffer.from(data);
});
To use fs
as above create fs
instance as below. 要使用fs
如上创建fs
如下实例。
var fs = require('fs');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.