express / connect-from表单事件监听器未触发

时间:2011-12-02 22:54:00

标签: node.js express

我最近将我的环境升级到Node v0.6.3,并且一些文件上传功能在停止工作之前工作正常。我将文件上传表单发布到以下页面:

var fs = require('fs'),
    client = require('../app'),
    exec = require('child_process').exec,
    socket = require('../socket')

var supported = [ 'image/jpeg',
                  'image/pjpeg',
                  'image/bmp',
                  'image/gif',
                  'image/tiff',
                  'image/png']

var maxSize = 204800;

module.exports = function(req, res) {
  console.log(JSON.stringify(req.form));
  var tmpPath = '';
  req.form.on('fileBegin', function(err, fileInfo) {
    console.log('upload file begin')
    if (fileInfo.path) {tmpPath = fileInfo.path};
    socket.emit('attachment_upload_started', req.query.user, {friend: req.query.friend}, []);
  });

  req.form.on('progress', function(bytesReceived, bytesExpected) {
    var percent = (bytesReceived / bytesExpected * 100) | 0;
    socket.emit('attachment_progress', req.query.user, {progress: percent, friend: req.query.friend}, []);    
       var child = exec('file --mime-type ' + tmpPath, function (err, stdout, stderr) {
          var mimetype = stdout.substring(stdout.lastIndexOf(':') + 2, stdout.lastIndexOf('\n'));
          if (!err && mimetype && supported.indexOf(mimetype)==-1 && mimetype!='Can\'t read SAT') {
            socket.emit('attachment_error', req.query.user, {type: 'format', friend: req.query.friend}, []);
            req.connection.destroy();
            req.connection.resume = function(){};
            console.log(mimetype)
            console.log(err);
          }

          if (bytesExpected > maxSize) {
            socket.emit('attachment_error', req.query.user, {type: 'size', friend: req.query.friend}, []);
            req.connection.destroy();
            req.connection.resume = function(){}
          }
        });
      });

  req.form.complete(function(err, fields, files){
    console.log('form complete');
    if (!err) {
      if (supported.indexOf(files.upload.mime)==-1) {
        console.log(files.upload.mime);
        socket.emit('attachment_error', req.query.user, {type: 'format', friend: req.query.friend}, []);
      }
      else {
        socket.emit('attachment_complete', req.query.user, {friend: req.query.friend, file: files.upload}, []);
      }
    }
    else {
      console.log('err');
    }
    res.redirect('/iattachment?user='+req.query.user+'&friend='+req.query.friend);
  });
}

该文件确实进入了我的/ tmp目录,但是req.form.complete或req.form.on('progress')中没有任何代码,req.form.on('fileBegin')听众被执行。任何帮助,将不胜感激!

2 个答案:

答案 0 :(得分:3)

您使用的是express.bodyParser吗?

问题是连接,表达的模块是基于的,现在包括版本2.0的强大,但没有人打扰更改文档或示例。 (实际上,我现在就这样做。)

如果您已经在使用bodyParser,则已经创建了临时文件,并且由于已经处理了请求并且所有流都已读取,因此您的代码实际上并没有执行任何操作,并等待输入。

检查“req.body”,它应包含所有文件信息。否则尝试删除express.bodyParser。

答案 1 :(得分:0)

您需要bodyParser来解析POST数据。

尝试查看此http://senchalabs.github.com/connect/middleware-bodyParser.html

delete express.bodyParser.parse ['multipart / form-data'];

我还没能完成这项工作。我试图直接使用强大的,所以我可以发送进度条。但根本没有触发任何东西。