multiparty.Form()
无效。我正在尝试打印。 (例如2,3,4)
这是我的图片上传代码:
app.post('/gallery/add',function(req, res,next) {
var input = JSON.parse(JSON.stringify(req.body));
var multipart = require('connect-multiparty');
var multiparty = require('multiparty');
var format = require('util').format;
var fs = require("fs");
var path = require('path');
var tempPath =req.files.image.path;
var filename2 =req.files.image.originalFilename;
req.getConnection(function (err, connection) {
console.log('1');
var form = new multiparty.Form();
var image;
var title;
form.on('error', next);
form.on('close', function(err, fields, files){
console.log('2');
if(err) {
next(err);
console.log(err);
} else {
console.log('3');
ins = fs.createReadStream(tempPath);
ous = fs.createWriteStream(__dirname + '/uploads/' + image.filename);
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else {
res.redirect('/#gallery/add');
res.end();
}
});
//console.log('\nUploaded %s to %s', files.photo.filename, files.photo.path);
//res.send('Uploaded ' + files.photo.filename + ' to ' + files.photo.path);
}
res.send(format('\nuploaded %s (%d Kb) as %s'
, image.filename
, image.size / 1024 | 0
, title));
});
// listen on field event for title
form.on('field', function(name, val){
console.log('4');
if (name !== 'title') return;
title = val;
});
// listen on part event for image file
form.on('part', function(part){
console.log('5');
if (!part.filename) return;
if (part.name !== 'image') return part.resume();
image = {};
image.filename = part.filename;
image.size = 0;
part.on('data', function(buf){
image.size += buf.length;
});
});
// parse the form
form.parse(req);
});
});
似乎form.on
方法无效。
我哪里错了?
答案 0 :(得分:0)
只需使用它:
// parse a file upload
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;