2016-04-25 50 views
0

我試圖創建一個註冊表單,使用connect-busboy在node express中上傳照片。當我使用多部分/表單數據服務器不讀取表單中的其他文本字段。請任何人幫助我。節點表單註冊,文件上傳不起作用

我的玉文件,

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} 

我的註冊腳本,

router.post('/upload', function(req, res, next) 

    { 

    console.log("help..........."+req.body.chapter); 
    console.log("Got a POST request for the 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 to go next 
    }); 
}); 
}); 
+0

通常是多/形式 - 數據不是多部分/表單數據。另外,如果您向我們提供您使用的代碼,或者我們無法幫到您,那麼它會更好。 – jaumard

回答

0

的文件說,要檢索的字段必須添加:

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); 
+0

我嘗試過,但仍然存在問題。它顯示檢查未定義的錯誤。 – jicks

+0

看我的編輯。 req.pipe必須在busboy.on之後 – jaumard