formidable处理多文件上传

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

posted @ 2017-04-28 15:21  向着太阳生  阅读(394)  评论(0编辑  收藏  举报