NodeJS使用busboy上传文件

Posted 白云晨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
});

 

以上是关于NodeJS使用busboy上传文件的主要内容,如果未能解决你的问题,请参考以下文章

node.js上传下载pdf文件

如何在 JavaScript 中将 Blob 转换为文件

busboy 遇到问题并且找不到 Content-Type

Node / Busboy:获取文件大小

限制快递到特定路线

节点 js busboy 不使用 angular js $http post 发出事件