如何在一个请求中保存gridFS中的多个文件?

我正在使用nodejs和mongodb,并希望一次存储多个文件。 我正在使用GridFS的文件。

我的代码工作正常单个file upload。 var writestream = gfs.createWriteStream({filename:filename,metadata:project._id}); 的console.log(tmpFilepath);

var filename = req.files.file.name; var tmpFilepath="./upload/"+ guid(); fs.rename(req.files.file.path,tmpFilepath); fs.createReadStream(tmpFilepath) .on('end', function() { console.log("file Saved"); }) .on('error', function() { console.log("error encountered"); // res.send('ERR'); }) // and pipe it to gfs .pipe(writestream); writestream.on('close', function (file) { fs.unlink(tmpFilepath); }); 

我怎样才能使它上传多个文件?

把它包装在一个你可以安装的npm模块的aysnc调用中。

 var files = []; async.each([].req.files.name, function(item){ /* Apply your gridfs here */ files.push('somedetails'); }, function(err){ /* finished processing all the files...do something with them /* console.log(files); });