使用busboy解析文件流并直接写入expressF中的GridFs

时间:2016-03-03 06:55:58

标签: node.js express multipartform-data gridfs busboy

:)嗨!

我正在试图弄清楚如何在快速框架中将上传文件直接写入GridF。

我编写了如下代码,问题是文件事件“file”永远不会发出。

:(

var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();
var fs = require('fs');
var Busboy = require('busboy');

router.get('/test', function(req, res){
 res.render('test-gridfs', {title: 'TESTING GRIDFS'});
})

router.post('/test', function(req, res){
 var busboy = new Busboy({headers: req.headers});
 var gfs = req.gfs;

 busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  console.log('File [' + fieldname + ']: filename: ' + filename);
  file.pipe(gfs.createWriteStream({
   filename: 'moon.jpg',
   content_type: 'image/jpg'
  }))
 });
 busboy.on('finish', function() {
  res.json({result: 'finish'});
 });
 req.pipe(busboy);
})
module.exports = router;

0 个答案:

没有答案