Node Busboy abort upload

前端 未结 5 781
情书的邮戳
情书的邮戳 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 10:58

    I would think the proper thing to do was to just close the socket and end the request

    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    
        file.fileRead = [];
        var size = 0;
        file.on('data', function(chunk) {
    
            size += chunk.length;
    
            /* DO VALIDATION HERE */
            if( size > 500000) {
    
                self.req.socket.end();
                self.res.end();
    
            }
    
    
            file.fileRead.push(chunk);
        });
    
        file.on('end', function() {
            var data = Buffer.concat(file.fileRead, size);
            // ... upload to S3
        });
    
        self.req.pipe(busboy);
    });
    

提交回复
热议问题