node.js flatiron – 通过multipart / form-data上传文件

我有一个flatiron应用程序,现在需要进行扩展,以处理图像的多部分/表单数据上传。

如何处理file upload在熨斗应用程序? 工会/董事似乎忽略了多部分/forms数据,而我所有的强大集成的尝试都失败了 – 我认为这是因为在强大的请求对象之前联合执行的行为。

我已经尝试正常和streaming: true路线,以及before数组before原始处理。

我不能是唯一需要这个的人,所以可能已经解决了,我很抱歉。 我只是找不到任何参考资料。

你可以使用联合connect.multipart(或bodyParser),它已经使用节点强大。

 var connect = require('connect'), union = require('union'); var server = union.createServer({ buffer: false, before: [ connect.bodyParser(), function(req, res) { if (req.method==='POST'){ console.log(req.files); res.end('Done'); }else{ res.writeHead(200, { 'Content-Type': 'text/html' }); res.end('<form method="post" enctype="multipart/form-data">' + '<input type="file" name="file" />' + '<input type="submit" value="Upload" />' + '</form>'); } }, ] }).listen(8000); 

显然你必须在联盟选项中closures缓冲,而在你的端点的选项中是stream:

 var fs = require('fs'), path = require('path'), union = require('../../lib'), director = require('director'), favicon = require('./middleware/favicon'), // for uploading: formidable = require('formidable'), util = require('util'); var router = new director.http.Router(); var server = union.createServer({ buffer: false, before: [ favicon(path.join(__dirname, 'favicon.png')), function (req, res) { var found = router.dispatch(req, res); if (!found) { res.emit('next'); } } ] }); router.get('/foo', function () { this.res.writeHead(200, { 'Content-Type': 'text/html' }); this.res.end('<form action="/foo" enctype="multipart/form-data" method="post">'+ '<input type="text" name="title"><br>'+ '<input type="file" name="upload" multiple="multiple"><br>'+ '<input type="submit" value="Upload">'+ '</form>'); }); router.post('/foo', { stream: true }, function () { var req = this.req, res = this.res, writeStream; var form = new formidable.IncomingForm(); console.log('Receiving file upload'); form .on('field', function(field, value) { console.log(field, value); }) .on('file', function(field, file) { console.log(field, file); }) .on('progress', function(rec, expected) { console.log("progress: " + rec + " of " +expected); }) .parse(req, function(err, fields, files) { console.log('Parsed file upload' + err); res.writeHead(200, { 'Content-Type': 'text/plain' }); if (err) { res.end('error: Upload failed: ' + err); } else { res.end('success: Uploaded file(s): ' + util.inspect({fields: fields, files: files})); } }); }); server.listen(9090); console.log('union with director running on 9090');