天天看点

node.js + busboy 多文件上传

html

<form class="form form-signin" action="/videoUpload" method="post" enctype="multipart/form-data">
       
    <div class="video">
        <span>添加视频</span>
        <input type="file" name="videoUpload"  id="cVideo"  accept="video/*" />
        <p class="vBackg"></p>
    </div>

    <input type="submit" value="上传课程视频" class="btn btn-primary btn-block submit" />
</form>
           

index.js:

router.post('/videoUpload', function(req, res) {
   // req.pipe(req.busboy);
    var fileNum = 3,
        fileCount = 0,
        filePath = path.join(path.normalize(__dirname + '/..'), 'public', 'upload');
    var busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname + ']: filename: ' + filename);
        file.on('data', function(data) {
            console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
        });
        file.on('end', function() {
            var fstream;
            if(filename !== ""){
                fstream = fs.createWriteStream(filePath+'/'+filename.trim());
                file.pipe(fstream);
            }
            console.log('File [' + fieldname + '] Finished');
        });
        });
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
        console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    });
    busboy.on('finish', function() {
        console.log('Done parsing form!');
        res.writeHead(303, { Connection: 'close', Location: '/' });
        res.end();
    });
    req.pipe(busboy);
});
           

继续阅读