busboy没有解雇完成事件

时间:2014-07-05 16:28:59

标签: javascript node.js express

我正在尝试将路由中的请求对象传递给处理上传的控制器,

这是路线 -

app.post('/upload/notes',auth.requiresApiLogin,function(req,res){
        upload.file(req,res);
});

这是控制器(upload.js)代码,它具有导出的文件方法

var fs = require('fs'),
    uuid = require('node-uuid'),
    path = require('path'),
    Busboy = require('busboy');
exports.file = function(req, res) {
    var busboy = new Busboy({ headers: req.headers});
    busboy.on('file', function(fieldname, file, filename,transferEncoding,mimeType) {
        console.log("inside upload function");
        console.log(file);
    });
    busboy.on('field', function(fieldname, val, valTruncated,keyTruncated) {
        console.log("inside field function");
        console.log(fieldname);
    });
    busboy.on('finish',function(){
        console.log('finished');
    });
    req.pipe(busboy);
//
//    req.pipe(req.busboy);
//    req.busboy.on('file', function(fieldname, file, filename,transferEncoding,mimeType) {
//        var fName = uuid.v4();
//        console.log(filename);
//        var fileext = filename.substr(filename.lastIndexOf('.') + 1);
//
//        console.log(transferEncoding);
//        console.log(mimeType);
//        var filepath = path.normalize(__dirname + '/../../');
//        var fstream = fs.createWriteStream(filepath+'/server/uploads/'+fName+'.'+fileext);
//        file.pipe(fstream);
//        fstream.on('close', function () {
//            res.redirect('back');
//        });
//    });
};

所以,我看到字段和文件都在控制台中记录,但是完成事件没有被触发。我还应该尝试什么?

2 个答案:

答案 0 :(得分:22)

您需要以某种方式使用file流。出于测试目的,您可以通过在文件事件处理程序中添加file.resume();来忽略数据。

答案 1 :(得分:0)

这个答案可能对想要使用该文件的人有所帮助...

确保在消费完文件后通过在完成方法中调用 next() 将控制权传回给 express ,例如

app.use(function(req: Request, res: ServerResponse, next:any) {
let bb = new busboy({ headers: req.headers });
let fileData: any = null;
const fieldArray: any = [];
let dataLength: number;
const extractFields =  (name: string, val: any, data: any) => {
  if (Array.isArray(data[name])) {
    data[name].push(val);
  } else if (data[name]) {
    data[name] = [data[name], val];
  } else {
    data[name] = val;
  }
};
bb.on('field', function(fieldname: string, val:any, fieldnameTruncated: boolean, valTruncated: boolean, encoding: string, mimetype: string) {
  // extract fields to add to req body for later processing
  extractFields(fieldname, val, fieldArray);
  console.log('Field [' + fieldname + ']: value: ' + val);

});

bb.on('file', function(fieldname: string, file: NodeJS.ReadableStream , filename: string, encoding: string, mimetype: string) {
  file.on('data', function(data) {
    console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
    // Process buffer to save complete file to add to the req for access later
    if (fileData === null) {
      fileData = data;
    } else {
      fileData = Buffer.concat([fileData, data]);
    }
  });
  file.on('end', function() {
    console.log('File [' + fieldname + '] Finished');
  });
});

bb.on('finish', function() {
  req.body = fieldArray
  req.body.files = {file: fileData};
  console.log('Done parsing form!');
  next(); // <- Allows processing to continue and avoids request hanging
});

bb.on('close', () => {
console.log('Event closed')
});
// what does this do? Send the readable request stream to busboy which is a writeable stream. The above code dictates to the writeable
// how to respond when certain events are fired during the pipe.
req.pipe(bb);
});