Ajax file upload with node.js

1,763 views
Skip to first unread message

feras ahmed

unread,
May 9, 2012, 2:03:09 PM5/9/12
to expre...@googlegroups.com

I'm trying to use ajax file upload plugin(enter link description here) to upload a file to node.js server. This is my client side code to initialize plugin:

$(function() {
   
/* dom ready */
 
var uploader = new qq.FileUploader({
   
// pass the dom node (ex. $(selector)[0] for jQuery users)
    element
: document.getElementById('uploader'),
   
// path to server-side upload script
    action
: '/newitem',
    allowedExtensions
: ['jpg', 'jpeg', 'png', 'gif'],
    sizeLimit
: 10000000
});
});

My server code is

  app.post('/newitem',function(req, res) {
     
if(req.xhr) {
        console
.log('Uploading...');
       
var fName = req.header('x-file-name');
       
var fSize = req.header('x-file-size');
       
var fType = req.header('x-file-type');
       
var ws = fs.createWriteStream('./'+fName)

        req
.on('data', function(data) {
            console
.log('DATA');
            ws
.write(data);
       
});
        req
.on('end', function() {
            console
.log('All Done!!!!');
            res
.writeHead(200, { 'Content-Type': 'text/html' });
            res
.end();
       
});
   
}

       
});

No I can't get progress update and the uploader doesn't give failed for upload while upload success. I think this is related to ajax server response Am I right? How could I fix it?

Thanks, Feras

Paul Vencill

unread,
May 9, 2012, 2:35:22 PM5/9/12
to expre...@googlegroups.com
Not sure I fully understand the behavior you're getting.  Are you successfully uploading files, simply not getting progress?  or is the file not uploading at all?

--
You received this message because you are subscribed to the Google Groups "Express" group.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/IlrHMnMEOR8J.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

feras ahmed

unread,
May 9, 2012, 10:29:48 PM5/9/12
to expre...@googlegroups.com

The files get successfully uploaded but I could see any progress in client side and even when the upload completed on the server client still show like uploading file.

Thanks,
Feras

On Wednesday, May 9, 2012 8:35:22 PM UTC+2, Paul wrote:
Not sure I fully understand the behavior you're getting.  Are you successfully uploading files, simply not getting progress?  or is the file not uploading at all?

To unsubscribe from this group, send email to express-js+unsubscribe@googlegroups.com.

Paul Vencill

unread,
May 10, 2012, 11:20:41 AM5/10/12
to expre...@googlegroups.com
There's no reason from the code you posted that your app should show progress. The response doesn't pump bytes every time you call 'write', it buffers them until the stream is flushed (on .end()).  The plugin you're using, if you look at their server-side code, has other server endpoints that report on the amount of the file that's been uploaded, by comparing the bytes on disk to the calculated total bytes. That's a separate request from the client's perspective.

To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/DkyNFFxWuc4J.

To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages