0

我正在尝试制作具有图像上传功能的注册表单,因此我通过帖子和 enctype = "multipart/form-data" 使用 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');

  }

每次尝试时它都会显示此错误
TypeError: Cannot read property 'on' of undefined on the line

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

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

4

3 回答 3

0

我以前使用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. 确保 mimetypes 有效
  4. 一旦找到超过 XMB (X.1MB) 的文件,则中止上传并移至下一个文件
  5. 如果文件满足所有检查,则将其存储在本地

这是公共要点的链接Upload Multiple Files with Busboy Nodejs ExpressJS

于 2017-02-26T20:45:20.670 回答
0

我改用 multer 并且它工作正常

https://github.com/expressjs/multer

于 2017-03-02T09:06:51.723 回答
0

在第一次查看busboy-doc之后,在我看来,因为 busboy 不是扩展请求的中间件,请参见代码片段:

var busboy = new Busboy({ headers: req.headers });
    busboy.on('file',...
于 2017-02-26T18:06:02.380 回答