使用 busboy meanstack undefined 上传图片

upload an image using busboy meanstack undefined

我正在尝试制作一个具有图片上传功能的注册表单,所以我通过 post 和 enctype = "multipart/form-data"[=13= 使用它来获取 ejs 端的值]

<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>

我使用 busboy 从服务器端处理它

  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');

  }

每次尝试都会显示此错误
类型错误:无法读取行

上未定义的 属性 'on'
req.busboy.on('file', function(fieldname,file, filename,encoding,mimeType)

你能告诉我这个问题是什么吗?

第一眼看到 busboy-doc 后,在我看来,因为 busboy 没有中间件,它正在扩展请求,请参见代码片段:

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

我以前用过busboy上传图片,我会和你分享配置和它是如何完成的。

  1. 导入busboy模块

    var Busboy = require('busboy');

  2. 然后在你的 post 端点声明 busboy 对象

    var busboy = new Busboy({
      headers: req.headers,
      limits: {
        fileSize: 6*1024*1024 //2MB limit
      }
    });
    
  3. 后面是 post 端点内的其余代码

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

我在项目中创建了一个要点,其中我使用 busboy 通过以下检查上传多张图片

  1. 从上传表单中提取输入字段
  2. 确保文件大小没有超过指定的 XMB
  3. 确保 mime 类型有效
  4. 一旦找到超过 XMB (X.1MB) 的文件,就会中止上传并移至下一个文件
  5. 如果文件满足所有检查,则将其存储在本地

这是 link 到 public 要点 Upload Multiple Files with Busboy Nodejs ExpressJS

我改用了 multer,它工作正常

https://github.com/expressjs/multer