2017-02-26 101 views
0

我想要使用圖像上傳能力註冊表單,所以我採取在ejs一側使用這個帖子和enctype =「multipart/form-data」的值上傳圖像使用busboy meanstack undefined

<form method="post" action= "/SignUp" enctype="multipart/form-data" > 
    <div class="form-group"> 
     <label for="firstName">First name</label> 
     <input type="text" name="firstName" id="firstName" class="form-control" value="<%= locals.firstName || '' %>" required /> 
    </div> 
    <div class="form-group"> 
     <label for="lastName">Last name</label> 
     <input type="text" name="lastName" id="lastName" class="form-control" value="<%= locals.lastName || '' %>" required /> 
    </div> 
    <div class="form-group"> 
     <label for="username">Username</label> 
     <input type="text" name="username" id="username" class="form-control" value="<%= locals.username || '' %>" required /> 
    </div> 
    <div class="form-group"> 
     <label for="password">Password</label> 
     <input type="password" name="password" id="password" class="form-control" required /> 
    </div> 
    <div class = "from-group"> 
     <label for = "Image">Image</label> 
     <input Content-Type = "multipart/form-data" type ="file" name = "Image" id = "Image" class = "form-control" required/> 
    </div 
    <br /> 
    <br /> 
    <div class="form-group"> 
     <button type="submit" class="btn btn-primary">Register</button> 
     <a href="/login" class="btn btn-link">Cancel</a> 
    </div> 
</form> 

,我使用打雜

SignUp:function(req,res){ 
    let reg = new Registrations(); 
    var busboy = new Busboy({ 
    headers: req.headers, 
    limits: { 
     fileSize: 6*1024*1024 //2MB limit 
    } 
    }); 
    var stream; 
    var fstream; 
     busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) { 
     if(fieldname == 'firstName') 
     reg.firstName = val; 
     else if (fieldname == 'lastName') 
      reg.lastName = val; 
      else if(fieldname == 'username') 
      reg.username = val; 
      else { 
      reg.password = val; 
      } 


     }) 
     busboy.on('file', function(fieldname,file, filename,encoding,mimeType){ 
     stream = __dirname + '/img/' + filename; 
     fstream = fs.createWriteStream(__dirname + '/img/' + filename); 
     file.pipe(fstream); 
     fstream.on('close', function(){ 
      reg.Image = stream; 
     reg.save(function(err,reg){ 
      if(err){ 
      res.send(err.message) 
      console.log(err); 
      }else{ 
      console.log(reg); 
      } 
     }) 
     }) 
    }) 
    busboy.on('finish', function() { 

    }) 
    res.render('login'); 

    } 

處理它從服務器端每一次我嘗試它表明我這個錯誤是
類型錯誤:無法讀取「開」的未定義的屬性就行了

req.busboy.on('file', function(fieldname,file, filename,encoding,mimeType) 

你能告訴我這是什麼問題嗎?

+0

現在它不顯示錯誤了,但這些字段沒有收到,我可以」 t將它們保存在貓鼬 –

回答

0

第一看看到busboy-doc後,在我看來,作爲打雜沒有中間件,這是擴展請求,請參見代碼片段:

var busboy = new Busboy({ headers: req.headers }); 
    busboy.on('file',... 
+0

仍在同一行上顯示相同的錯誤 –

0

我以前用過打雜上傳圖像,我會與你分享配置以及如何完成。

  1. 導入busboy的模塊

    busboy的變種需要=( 'busboy的');

  2. 然後你端點聲明打雜對象內部

    var busboy = new Busboy({ 
        headers: req.headers, 
        limits: { 
        fileSize: 6*1024*1024 //2MB limit 
        } 
    }); 
    
  3. 其次

    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) { 
         //extract intput-field from upload-form 
    }); 
    
    
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { 
         //process each file upload, check for size, mimetype, 
         //store if all checks passed, delete otherwise, move to 
         //next file 
    }); 
    
    
    //finish call back when all files are uploaded 
    busboy.on('finish', function() { 
        //do cleanup and other related work 
    }); 
    
    return req.pipe(busboy); 
    

我創建了一個代碼的後端點裏面休息依靠我使用的項目對於下列檢查

  1. 提取從上傳表單的輸入域上傳多張圖片打雜
  2. 確保文件大小不超過規定的XMB
  3. 確保MIME類型都有效
  4. 作爲只要找到一個文件超過XMB(X.1MB),然後中止上載並移動到下一個文件
  5. 如果文件滿足所有檢查,然後將其存儲在本地

這裏是鏈接到公共要點Upload Multiple Files with Busboy Nodejs ExpressJS

+0

什麼是on('field',...)爲? –

+0

這是將提取上傳表單的輸入字段,基本上不是類型文件的任何字段(即firstName,lastName等)的回調 – Raf

+0

我改編了我的基於你的模塊。錯誤消失了,但它現在甚至沒有收到這些字段(名字,等等......)。你認爲我從html端正確處理它?我編輯了我的問題的代碼,如果你想看看 –