首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NodeJS和BusBoy上传文件

使用NodeJS和BusBoy上传文件
EN

Stack Overflow用户
提问于 2017-05-04 23:48:06
回答 3查看 8.2K关注 0票数 1

我正在使用NodeJS上传文件。我的要求是将流读入一个变量,以便将其存储到AWS SQS中。我不想将文件存储在磁盘上。这个是可能的吗?我只需要上传到流中的文件。我使用的代码是(upload.js):

代码语言:javascript
复制
var http = require('http');
var Busboy = require('busboy');

module.exports.UploadImage = function (req, res, next) {

    var busboy = new Busboy({ headers: req.headers });

    // Listen for event when Busboy finds a file to stream.
    busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {

        // We are streaming! Handle chunks
        file.on('data', function (data) {
                // Here we can act on the data chunks streamed.
        });

        // Completed streaming the file.
        file.on('end', function (stream) {
            //Here I need to get the stream to send to SQS
        });
    });

    // Listen for event when Busboy finds a non-file field.
    busboy.on('field', function (fieldname, val) {
            // Do something with non-file field.
    });

    // Listen for event when Busboy is finished parsing the form.
    busboy.on('finish', function () {
        res.statusCode = 200;
        res.end();
    });

    // Pipe the HTTP Request into Busboy.
    req.pipe(busboy);
};

如何获取上传的流?

EN

回答 3

Stack Overflow用户

发布于 2017-07-30 06:50:28

在busboy 'file‘事件中,你会得到名为'file’的参数,这是一个流,所以你可以通过管道传输它。

例如

代码语言:javascript
复制
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { 
    file.pipe(streamToSQS)
}
票数 1
EN

Stack Overflow用户

发布于 2017-08-11 14:36:38

我希望这会对你有所帮助。

代码语言:javascript
复制
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
    var filename = "filename";
    s3Helper.pdfUploadToS3(file, filename);
  }
busboy.on('finish', function () {
        res.status(200).json({ 'message': "File uploaded successfully." });
    });
    req.pipe(busboy);
票数 1
EN

Stack Overflow用户

发布于 2018-10-22 21:48:49

虽然current和existing参数假设人们实际上可以将流( file )发送到可以接收流的某个地方,但实际的块是在您实现的file回调方法中接收的。

来自文档:(https://www.npmjs.com/package/busboy)

代码语言:javascript
复制
file.on('data', function(data) {
   // data.length bytes seems to indicate a chunk
   console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
});
      
   
file.on('end', function() {
  console.log('File [' + fieldname + '] Finished');
});

更新:

找到构造函数文档,第二个参数是一个可读的流。

文件(< string >字段名,< ReadableStream >流,< string >文件名,< string >传输编码,< string >mimeType) -为找到的每个新文件格式字段发出。transferEncoding包含文件流的“Content-Transfer-Encoding”值。mimeType包含文件流的“Content-Type值”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43787534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档