busboy-connect 在保存文件结束前完成时触发(node.js,表示)

busboy-connect fires on finish before the end of save file (node.js , express)

我使用 busboy connect 从我的客户端获取上传数据。我尝试保存数据,然后将 on.finish 到 return 状态保存到服务器。问题是 on.finish 在文件保存结束之前触发。 是我做错了什么还是模块就是这样工作的?

服务器端代码:

        console.log("upload started dirname ", __dirname);
        var fstream;
        var result = [];
        var number_of_files = 1;
        req.pipe(req.busboy);
        req.busboy.on('field', function(fieldname, val) {
            field_obj = {}
            field_obj[fieldname] = val;
            result.push(field_obj);
            console.log(field_obj)
        });
        req.busboy.on('file', function(fieldname, file, filename) {
            console.log("Uploading: " + filename);
            //Path where image will be uploaded
            if (result.length > 0) {
                var file_type = filename.substr(filename.length - 4);
                filename = result[0].name + '_' + number_of_files + file_type;
                number_of_files++;
            }
            fstream = fs.createWriteStream(__dirname + "/" + filename);
            file.pipe(fstream);
            fstream.on('close', function() {
                console.log("Upload Finished of " + filename);
                result.push(filename);
                console.log("result ",result)
            });
        });
        req.busboy.on('finish', function() {
            console.log("form parsing finished")
            console.log("result finish",result)
            res.sendStatus(200)
        });

我希望 "result finish" 是我看到的最后一条消息,但我在我的控制台上收到以下消息:

Uploading: testSwf1.swf
Uploading: testSwf3.swf
form parsing finished
result finish [ { name: '123' }, { clickUrl: '234234' } ]
Upload Finished of 123_1.swf
result  [ { name: '123' }, { clickUrl: '234234' }, '123_1.swf' ]
Upload Finished of 123_2.swf
result  [ { name: '123' },
  { clickUrl: '234234' },
  '123_1.swf',
  '123_2.swf' ]

编辑 我也尝试了 on.end,但那个根本没有开火。

req.busboy.on('end', function() {
    console.log("DONE PARSING FORM")
    console.log("NEW result finish", result)
});

这是预期的行为。 finish 在从请求中读取最后一个数据时发出(这包括 any/all 发出的 file 流,它们是传入请求数据的子集)。

但是,如果您正在将 file 流写入磁盘,file 流可能仍有最后一块数据仍在内存中缓冲。这就是为什么在发出 finish 时文件可能没有完全写入磁盘的原因。

一个常见的解决方案是检查在 busboy finish 事件处理程序中设置的标志,并检查每个 fstream close 事件递增的计数器变量(比较它例如 result.length)。

根据mscdex的建议,我使用了一个变量来查找写入何时完成:

var fstream;
var result = [];
var number_of_files = 1;
var counter = 0; 
req.pipe(req.busboy);
req.busboy.on('field', function(fieldname, val) {
    field_obj = {}
    field_obj[fieldname] = val;
    result.push(field_obj);
    console.log(field_obj)
});
req.busboy.on('file', function(fieldname, file, filename) {
    counter++;
    console.log("Uploading: " + filename);
    //Path where image will be uploaded
    if (result.length > 0) {
        var file_type = filename.substr(filename.length - 4);
        filename = result[0].name + '_' + number_of_files + file_type;
        number_of_files++;
    }
    fstream = fs.createWriteStream(__dirname + "/" + filename);
    file.pipe(fstream);
    fstream.on('close', function() {
        counter--;
        console.log("Upload Finished of " + filename);
        result.push(filename);
        console.log("result ",result)
        if(counter == 0){
            console.log("writing finished");
            res.sendStatus(200);
        }
    });
});