在nodejs上评估文件上传器 - 多个文件上传 [英] Valums file-uploader on nodejs - multiple file upload

查看:128
本文介绍了在nodejs上评估文件上传器 - 多个文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 valums ajax file-uploader



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

  fileStream = fs.createWriteStream(__ dirname +'/ .. / public /图像/房屋/'+ 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;

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

解决方案

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

$ $ $ $ $ $ $ $ $ $ $' 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没有关闭连接,挂了2-3分钟,直到图片出现,让我知道。

I'm using valums ajax file-uploader

My nodejs server side looks like this:

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);
});

client side:

        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;

This all works for single file upload just great!

So imagine - i press on upload button, chose pic, it uploads really fast, shows up in screen. Now i want to upload another one. I choose pic, it uploads on server fast (i see it on server), i get back the response of new filename and success, i put the picture on screen with my append. But the picture does not show up. I try open in new tab just the pic and still nothing even though i see it on the server standing in the right dir. After like 3-5 min of waiting it just shows up, without even page refresh needed. Whats causing this behavior? Is it the piping and i need to call Mario to fix it or something else? :)

解决方案

Changed my req.pipe to req.on('data') and it started to work. Somehow it seems like my req.pipe didnt close connection and was "expecting" for more data to come even though all file was uploaded. That is why i could not call GET to file and it was in "pending" status. This fixed my problems:

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);
    }); 

Even though i found solution to my problem if anyone knows why req.pipe didnt close connection and was stuck hanging for like 2-3mins till the pic appeared, let me know.

这篇关于在nodejs上评估文件上传器 - 多个文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆