gpt4 book ai didi

javascript - nodejs,connect-busboy。上传几个文件

转载 作者:行者123 更新时间:2023-11-29 21:56:48 24 4
gpt4 key购买 nike

当通过 busboy 上传少量文件时,我遇到了事件问题。我的代码:

app.post('/multiupload', function(req, res) {
var fstream;
var files = [];
var busboy = new Busboy({headers: req.headers});
busboy.on('file', function (fieldname, file, filename) {
fstream = fs.createWriteStream(__dirname + '/../static/uploaded/' + filename);
file.pipe(fstream);
fstream.on('close', function(){
console.log('file ' + filename + ' uploaded');
files.push(filename);
});
});

busboy.on('end', function(){console.log('END')});

busboy.on('finish', function(){
console.log('finish, files uploaded ', files);
res.redirect('back');
});
req.pipe(busboy);
});

我的表单(Jade 模板)

form(method="POST", action="/multiupload" name="multiupload_form", enctype="multipart/form-data")
input(type='file' name='multifile', multiple)
input(type="submit" value="Upload!")

事件 'end' 被忽略,在文件上传过程中完成触发。我哪里错了?

服务器控制台报告:

file 111.gz uploaded
file 222.mp4 uploaded
file 333.jpg uploaded
finish, files uploaded [ '111.gz', '222.mp4', '333.jpg' ]
file 444 uploaded
file 555.jpg uploaded

最佳答案

busboy 不会发出 end 事件。 finish 事件在处理完整个请求并完全读取所有 file 流后发出。所以问题是底层文件描述符的关闭发生在 finish 发出之后的下一个滴答声(或左右)。

如果您需要知道所有文件描述符何时关闭,则需要想出一种方法来跟踪发出了多少 close 事件。

关于javascript - nodejs,connect-busboy。上传几个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162633/

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