Node Busboy abort upload

前端 未结 5 779
情书的邮戳
情书的邮戳 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条回答
  •  -上瘾入骨i
    2021-01-04 10:57

    I would try something like this:

    var self = this;
    var busboy = new Busboy({
        headers: self.req.headers,
        limits: {
            files: 1,
            fileSize: 500000
        }
    });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    
        file.fileRead = [];
    
        file.on('limit', function() {
            //Clear variables
        });
    
        file.on('data', function(chunk) {
            file.fileRead.push(chunk);
        });
    
        file.on('end', function() {
            var data = Buffer.concat(file.fileRead, size);
            // ... upload to S3
        });
    
        self.req.pipe(busboy);
    });
    

    Basically I added a new limit in the configuration of Busboy: fileSize: 500 * 1024

    And I started listening limit event:

        file.on('limit', function() {
            //Clear vars
        });
    

提交回复
热议问题