我最近將環境升級到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')監聽器被執行。任何幫助,將不勝感激!
是的,我正在使用bodyParser。刪除它似乎已經完成了這個伎倆,但破壞了其他一些功能。謝謝你的幫助。 –