在nodejs上评估file upload器 – 多个file upload

我正在使用valums ajaxfile upload器

我的nodejs服务器端看起来像这样:

fileStream = fs.createWriteStream(__dirname+'/../public/images/houses/'+rndname); req.pipe(fileStream); req.on('end', function() { body = '{"success":"true", "name": "'+rndname+'"}'; res.writeHead(200, { 'Content-Type':'text/plain' , 'Content-Length':body.length }); res.end(body); }); 

客户端:

  function createUploader(){ var uploader = new qq.FileUploader({ element: document.getElementById('homepic'), action: '/server/upload', allowedExtensions: ['jpg', 'png', 'gif'], multiple:true, onComplete: function(id, fileName, responseJSON){ $("#homepic").append("<img src='/images/houses/"+responseJSON.name+"' class='mediumpic' /> "); } }); } window.onload = createUploader; 

这一切都适用于单个file upload只是伟大的!

所以想象 – 我按上传button,select图片,它上传真的很快,显示在屏幕上。 现在我想上传另一个。 我select图片,它在服务器上快速上传(我看到它在服务器上),我回来了新的文件名和成功的响应,我把我的追加在屏幕上的图片。 但图片没有显示出来。 我试图在新标签中打开只是图片,即使我看到它站在右侧的服务器上,仍然没有。 等待3-5分钟后才显示出来,甚至不需要刷新页面。 什么导致这种行为? 这是pipe道,我需要打电话给马里奥修理它或别的东西? 🙂

将我的req.pipe更改为req.on('data')并开始工作。 不知何故,似乎我的req.pipe没有closures连接,并且即使所有的文件都上传了,也会“等待”更多的数据。 这就是为什么我不能打电话给GET文件,它处于“挂起”状态。 这解决了我的问题:

 req.on('data', function(data) { ws.write(data); }); req.on('end', function(data) { var body = '{"success":"true", "name": "'+rndname+'"}'; res.writeHead(200, { 'Content-Type':'text/plain' , 'Content-Length':body.length }); res.end(body); }); 

即使我find解决我的问题,如果有人知道为什么req.pipe没有closures连接,被卡住悬挂像2-3分钟,直到图片出现,让我知道。

我创build了Express 3.x中间件组件,允许您通过req.files集合访问valums / fineuploader上传的文件。

您只需要在快速configuration期间添加中间件,如下所示:

 var fineuploaderExpressMiddleware = require('fineuploader-express-middleware'); app.use(express.cookieParser()); app.use(express.bodyParser()); app.use(fineuploaderExpressMiddleware({ uploadDir: '/tmp ' })); 

该组件在这里可用: https : //github.com/suprememoocow/fineuploader-express-middleware