node.js - Node Express Form registration with file upload not working -
i tried create registration form upload photo in node express using connect-busboy. when using multi-part/form-data server doesn't read other text fields in form. please 1 me.
my jade file is,
form(class='form-signin', action='/s/upload', method='post', enctype='multipart/form-data') input(type='text', name='classname', class='form-control', placeholder='classname',required) input(type='text', name='subject', class='form-control', placeholder='subject',required) input(type='text', name='chapter', class='form-control', placeholder='chapter',required) input(type='text', name='module', class='form-control', placeholder='module',required) input(type='text', name='name', class='form-control', value = user.username,required) input(type='file', name='thumbnail', class='form-control') input(type='text', name='regid', class='form-control',value = "#{user._id}") button(class='btn btn-lg btn-primary btn-block', type='submit') register span.clearfix #message if message h1.text-center.error-message #{message}
my registration script is,
router.post('/upload', function(req, res, next) { console.log("help..........."+req.body.chapter); console.log("got post request homepage"); var ar =dirname+'/uploads/'+req.body.thumbnail; var person = new upd({ classname: req.body.classname, subject: req.body.subject, chapter: req.body.chapter, module: req.body.module, name: req.body.name, thumbnail: req.body.thumbnail, filedtls: ar, regid: req.body.regid }); person.save(function (error, data){ if(error) {throw error; } }); var fstream; req.pipe(req.busboy); req.busboy.on('file', function (fieldname, file, filename) { console.log("uploading: " + filename); console.log("uploading: " + dirname); fstream = fs.createwritestream(dirname + '/uploads/' + filename); file.pipe(fstream); fstream.on('close', function () { console.log("upload finished of " + filename); console.log("file lo " + ar); res.redirect('/home'); //where go next }); }); });
the documentation retrieve fields have add :
req.busboy.on('file', function (fieldname, file, filename) { console.log("uploading: " + filename); console.log("uploading: " + dirname); req.busboy.on('field', function(fieldname, val, fieldnametruncated, valtruncated) { console.log('field [' + fieldname + ']: value: ' + inspect(val)); }); req.pipe(req.busboy);
Comments
Post a Comment