Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Valums file-uploader on nodejs - multiple file upload

Valums file-uploader on nodejs - multiple file upload

Problem

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("  ");
            }
        });           
    }
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? :)

Problem courtesy of: rskuja

Solution

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.

Solution courtesy of: rskuja

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Valums file-uploader on nodejs - multiple file upload

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×