2017-02-26 26 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端正确处理它?我编辑了我的问题的代码,如果你想看看 –