Javascript 节点XHR文件上载事件

Javascript 节点XHR文件上载事件,javascript,node.js,express,Javascript,Node.js,Express,考虑一个接收文件上传的ExpressJS应用程序: app.post('/api/file', function(req, res) { req.on('data', function() { console.log('asd') }) }) 我不明白为什么数据事件从未被触发。 我还使用了bodyParser()中间件,它为每个文件提供了以下对象,这些文件似乎有一些事件可用,但仍然无效: { file: { domain: null,

考虑一个接收文件上传的ExpressJS应用程序:

app.post('/api/file', function(req, res) {
    req.on('data', function() {
        console.log('asd')
    })
})
我不明白为什么数据事件从未被触发。 我还使用了bodyParser()中间件,它为每个文件提供了以下对象,这些文件似乎有一些事件可用,但仍然无效:

{
    file: {
        domain: null,
        _events: {},
        _maxListeners: 10,
        size: 43330194,
        path: 'public/uploads/a4abdeae32d56a2494db48e9b0b22a5e.deb',
        name: 'google-chrome-stable_current_amd64.deb',
        type: 'application/x-deb',
        hash: null,
        lastModifiedDate: Sat Aug 24 2013 20: 59: 00 GMT + 0200(CEST),
        _writeStream: {
            _writableState: [Object],
            writable: true,
            domain: null,
            _events: {},
            _maxListeners: 10,
            path: 'public/uploads/a4abdeae32d56a2494db48e9b0b22a5e.deb',
            fd: null,
            flags: 'w',
            mode: 438,
            start: undefined,
            pos: undefined,
            bytesWritten: 43330194,
            closed: true,
            open: [Function],
            _write: [Function],
            destroy: [Function],
            close: [Function],
            destroySoon: [Function],
            pipe: [Function],
            write: [Function],
            end: [Function],
            setMaxListeners: [Function],
            emit: [Function],
            addListener: [Function],
            on: [Function],
            once: [Function],
            removeListener: [Function],
            removeAllListeners: [Function],
            listeners: [Function]
        },
        open: [Function],
        toJSON: [Function],
        write: [Function],
        end: [Function],
        setMaxListeners: [Function],
        emit: [Function],
        addListener: [Function],
        on: [Function],
        once: [Function],
        removeListener: [Function],
        removeAllListeners: [Function],
        listeners: [Function]
    }
}

我想了解如何使进度和完成事件工作。

我不认为存在任何与express req对象绑定的数据事件。你在哪里看到的

此外,请尝试在您用于上载的表单上添加以下内容:

enctype="multipart/form-data"

在Express中访问请求和响应对象时,请求已结束,上载已完成。因此,
data
事件将不再触发,因为没有更多的数据可接收(并且根据Jonathan Ong的评论,可读流已被消耗)。查找文件上载进度的另一种方法是使用中间件,特别是body解析器

查看Connect文档时(因为Express是基于Connect构建的),它指出:

defer
延迟处理并将强大的表单对象公开为 调用
req.form.next()
,而无需等待表单的“end”事件。 如果需要绑定到“progress”事件,此选项非常有用

因此,在初始化主体解析器时,您只需将set
defer
添加为true,然后就可以监听
requ.form
progress
事件。示例如下:

app.use(express.bodyParser({
  defer: true              
}));

app.post('/upload', function (req, res) {
  req.form.on('progress', function (received, total) {
    var percent = (received / total * 100) | 0;
    console.log(percent + '% has been uploaded.');
  });

  req.form.on('end', function() {
    console.log('Upload completed.');
  });
});

您是否有文档指示您编写此代码?我用xmlhttprequest而不是普通的html表单上传文件。然而,req对象上的数据事件是可用的,所以一旦任何请求传入(?)主体已经在主体解析器中间件上解析,它就会触发。删除它,您将获得数据。req对象是一个流。当然,它有数据事件。主体解析器只使用it.req,res仍然是streams/eventemitter。可读流刚刚被消耗。