2011-11-24 208 views
4

我使用valums ajax file-uploaderValums文件上传 - 多文件上传

我的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; 

这一切工作单个文件上传很棒!

所以想象 - 我按上传按钮,选择图片,它上传真的很快,显示在屏幕上。 现在我想上传另一个。我选择图片,它在服务器上快速上传(我在服务器上看到它),我得到新文件名和成功的响应,我把图片放在屏幕上,并附上我的附件。但图片没有显示出来。我尝试在新标签中打开图片,尽管我在站在右侧的服务器上看到它仍然没有。等待3-5分钟后,它就会显示出来,甚至不需要刷新页面。什么导致这种行为?这是管道,我需要打电话给马里奥修理它或其他东西? :)

+0

按照[GitHub的页面(https://开头github.com/valums/file-uploader)这个项目已经转移到http://fineuploader.com/上面的链接中断。 – Sukima

回答

4

将我的req.pipe更改为req.on('data')并开始工作。不知何故,似乎我的req.pipe没有关闭连接,并且即使所有文件都已上传,也期待更多的数据来临。这就是为什么我不能打电话给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); 
    }); 

即使我找到解决我的问题,如果有人知道为什么req.pipe didnt密切的联系,并坚持挂像2-3mins直到PIC出现,让我知道。

1

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

所有你需要做的是你的Express配置过程中添加的中间件,像这样:

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