【NodeJS】使用busboy上传文件

router.post('/upload', function (req, res) {
    var busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
        var saveTo = path.join("C:/TestProject/node.js/TestExpress/TestExpress/TestExpress/", 'upload', path.basename(filename));
        console.log(saveTo);
        file.pipe(fs.createWriteStream(saveTo));
        console.log('jax');
    });
    busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
        console.log('Field [' + fieldname + ']: value: ' + val);
    });
    busboy.on('finish', function () {
        console.log('Done parsing form!');
        res.writeHead(200);
        res.end('upload OK!');
    });
    req.pipe(busboy);
});

 

posted @ 2016-06-12 23:16  人生无赖  阅读(3265)  评论(0编辑  收藏  举报