【发布时间】:2014-07-30 11:54:12
【问题描述】:
发布多部分表单时,我可以使用 connect-busboy 检索文件,但无法从 req.body 获取值。我假设我需要挂钩 req.busboy.on('field'),但不知道将其放置在哪里,以便我仍然可以利用当前正在处理上传文件的功能。
routes.js
var busboy = require('connect-busboy');
module.exports = function (app) {
app.use(busboy());
app.route('/upload')
.post(function (req, res) {
req.pipe(req.busboy);
upload.createImg(req, res);
});
upload.js
var fs = require('fs');
exports.createImg = function (req, res) {
var fstream,
path = './uploads/temp/';
req.busboy.on('file', function (fieldname, file, filename) {
fstream = fs.createWriteStream(path + filename);
file.pipe(fstream);
fstream.on('close', function() {
fs.readFile(path + filename, function (err, data) {
// need help here
});
});
});
});
在上面的代码示例中,我能够检索上传的文件,然后我可以使用 ImageMagick 成功操作这些文件。然而,问题是我想从 req.body 中检索数据,例如 req.height、req.width 等。不过,似乎 busboy 还没有完成它的魔力,因为 req.body 是 undefined。
如何将req.body 传递给fstream.on('close', ...) 中的函数?
【问题讨论】:
标签: node.js file-upload express multipartform-data