Help with Formidable

617 views
Skip to first unread message

Phil Daws

unread,
Feb 9, 2012, 12:35:32 PM2/9/12
to nod...@googlegroups.com
Hello,

I am attempting to write my first app that simply allows a file to be uploaded. When I click on the submit button the browser appears to start to post but just sits there. The app code I am using is:

app.post('/upload', function(request, response) {
  
  var form = new formidable.IncomingForm();
  
  form.uploadDir = __dirname + '/uploads';

  logger('Upload dir = ' + form.uploadDir);
 
  form.on('file', function(field, file) {
    logger('Receving file - ' + file.name);
    fs.rename(file.path, form.uploadDir + "/" + file.name);
  });

  form.on('error', function(err) {
    logger("an error has occured with form upload");
    logger(err);
    request.resume();
  });

  form.on('progress', function(bytesReceived, bytesExpected) {
    logger('Received: ' + bytesReceived + ' Expected: ' + bytesExpected);
  });

  form.on('aborted', function(err) {
    logger("user aborted upload");
  });

  form.on('end', function() {
    logger('upload completed');
  });
    
  form.parse(request, function() {
    response.render('/upload');
  });
}); 

and in the jade template I have:

h1= title
p #{title}

form(action="/upload", method="post", enctype="multipart/form-data")
  span
    input(class="uploadselection", type="file",   name="uploadFile")
    input(class="uploadbutton",    type="button", value="Upload", name="upload", onClick="submit();")
    input(class="uploadbutton",    type="button", value="Cancel Upload", name="cancel")

What I see on the console is:

9 Feb 17:28:10 - [nodemon] starting `node app.js`
   info  - socket.io started
Express server listening on port 8080 in development mode
9 Feb 17:28:14 - Connect from 127.0.0.1
9 Feb 17:28:19 - Disconnection from 127.0.0.1
9 Feb 17:28:19 - Upload dir = /home/uxbod/Development/nodeapps/fileupload/uploads

What am missing please ? I do not even see any data received :(
--
Thanks, Phil

Phil Daws

unread,
Feb 9, 2012, 3:08:12 PM2/9/12
to nod...@googlegroups.com
I have studied the example upload.js and change my code slightly based on that but it still just hangs when I post :( If I try the example code that works fine. Must be missing something silly :(

app.get('/upload', routes.upload);

app.post('/upload', function(request, response) {

  var form = new formidable.IncomingForm(), files = [], fields = [];
  form.uploadDir = __dirname + '/uploads';  
  
  logger('Processing form');

  form
  
  .on('file', function(field, file) {
    logger('Receving file - ' + file.name);
    files.push([field, file]);
  })

  .on('field', function(field, value) {
    console.log(field, value);
    fields.push([field, value]);
  })

  .on('error', function(err) {
    logger("an error has occured with form upload");
    logger(err);
    request.resume();
  })

  .on('progress', function(bytesReceived, bytesExpected) {
    logger('Received: ' + bytesReceived + ' Expected: ' + bytesExpected);
  })

  .on('aborted', function(err) {
    logger("user aborted upload");
  })

  .on('end', function() {
    logger('-> upload done');
    result.writeHead(200, {'content-type': 'text/plain'});
    result.write('received fields:\n\n ' + util.inspect(fields));
    result.write('\n\n');
    result.end('received files:\n\n ' + util.inspect(files));
  });   

  form.parse(request);
}); 

--
Thanks, Phil


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

tjholowaychuk

unread,
Feb 9, 2012, 5:26:28 PM2/9/12
to nodejs
if you're using bodyParser read the bottom of this:
http://www.senchalabs.org/connect/middleware-bodyParser.html
it will otherwise handle multipart for you

Phil Daws

unread,
Feb 10, 2012, 6:21:04 AM2/10/12
to nod...@googlegroups.com
Perfect, thank you :)
Reply all
Reply to author
Forward
0 new messages