首先,在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中的元素。
