Node Busboy abort upload

前端 未结 5 809
情书的邮戳
情书的邮戳 2021-01-04 10:37

I\'m using busboy, writing my uploaded file to a buffer and performing some validation on it (width, height and filesize). I can\'t for the life of me figure out how to abo

5条回答
  •  死守一世寂寞
    2021-01-04 11:02

    Ok, so I had the same problem and I solved it with file.resume();

    var fstream;
    req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
    
        // Validate file mimetype
        if(mimetype != 'image/png'){
            file.resume();
            return res.json({
                success: false,
                message: 'Invalid file format'
            });
        }
    
        // Upload
        fstream = fs.createWriteStream(__dirname + '/tmp/' + timestamp + filename);
        file.pipe(fstream);
        fstream.on('close', function () {
            return res.json({
                success: true
            });
        });
    
    });
    
    req.pipe(req.busboy);
    

提交回复
热议问题