使用Sails js无法将缓冲区识别为Node.js上的缓冲区

时间:2018-10-04 23:07:23

标签: javascript node.js stream sails.js buffer

我正试图从发送到SailsJs服务器的Blob中获取缓冲区。

正在发送到服务器的示例如下:

  

Blob(3355){大小:3355,键入:“ 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)
...

在这种情况下,错误位于body.push(new Buffer(chunk));上的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?中得到了一些指导

任何人都可以帮助我从该buffer中获取req.file

2 个答案:

答案 0 :(得分:0)

您当前的上传方法可以使用,但是您可能还需要考虑另一种新方法:

// 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)));

Full Example Here

还可以使用示例项目Ration查看Sails.JS Platzi Course的视频教程,了解有关此最新上传功能的信息。

答案 1 :(得分:0)

您可以按以下方式获得ul.livability-menu { display: block; height: 50px; padding: 10px 15px; width: 100%; line-height: 2.5em; box-sizing: border-box; background-color: #0066CC; } .more { float: right; text-align: right; color: #ffffff; } ul.dropDown { width: 100%; box-sizing: border-box; list-style-type: none; position: relative; padding: 17px; font-size: 1.3em; visibility: hidden; top: 0px; z-index: 1; background-color: #0066CC; -webkit-transition: all .25s; -moz-transition: all .25s; -ms-transition: all .25s; -o-transition: all .25s; transition: all .25s; } ul.livability-menu:hover .dropDown { list-style-type: none; visibility: visible; width: 100%; box-sizing: border-box; line-height: 3em; z-index: 1; background-color: #0066CC; } li.livability { font-size: 1.6em; color: #ffffff; list-style-type: none; background-color: #0066CC; } a.livability-a { text-decoration: none; color: #fff; } a.livability-a:hover { color: #EBF907; } a.livability-a:visited { text-decoration: none; color: #96D2F8; }

uploadedFile

您可以从req.file('recordingPart').upload(function (err, uploadedFiles){ if (err) return res.serverError(err); // Logic with uploadedFiles goes here }); 获取文件描述符,并使用它按如下所示读取/流式传输文件。

uploadedFiles[0].fd

要如上所述使用fs.readFile(uploadedFiles[0].fd, 'utf8', function (err,data) { // Logic with data goes here var myBuffer = Buffer.from(data); }); ,请创建如下的fs实例。

fs