gpt4 book ai didi

node.js - 如何在一次请求中将多个文件保存在gridFS中?

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:44 25 4
gpt4 key购买 nike

我正在使用nodejs和mongodb,并且想要一次存储多个文件。我正在使用 GridFS 来存储文件。

我的代码对于单个文件上传工作正常。 var writestream = gfs.createWriteStream({ 文件名: 文件名, 元数据: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 模块。

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);
});

关于node.js - 如何在一次请求中将多个文件保存在gridFS中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com