formidable处理多文件上传

时间:2023-03-08 16:50:15

首先,在html页面中,表单上传文件的控件需要加上multiple选项,或者multiple="multiple".

然后,在nodejs程序中处理post数据的路路由中使用formidable格式化表单

var form = new formidable.IncomingForm();
form.uploadDir = configs.productPath;
form.keepExtensions = true;
var files = []; form.on('file', function (filed, file) {
files.push([filed, file]);
});//whenever a file is received, this will add the file info to the array form.parse(req, function (err, fields) {
assert.equal(err, null);
//traverse the files here
}
});

通过form.on语句将所有上传的文件加入到files里。

然后,使用array.foreach遍历files中的元素。