图像不使用multer上传

时间:2017-12-10 00:42:24

标签: node.js mongodb multer

好的,所以在我的应用程序中,我试图允许使用multer上传图像。我的应用程序是使用node.js构建的,我的数据库是使用mongodb。当我创建一个帐户并为头像图像选择图像时,它会创建帐户,但会自动使用我设置的no-image.png文件,以防有人选择图像。这是代码......任何帮助都会很棒。

    // handle signup logic
    router.post("/register", function(req, res) {
      upload(req, res, function(err) {
        if(err){
          req.flash("error", err.message);
          return res.redirect("/register");
        }
        var newUser = new User({
          username: req.body.username,
          firstName: req.body.firstName,
          lastName: req.body.lastName,
          email: req.body.email,
          bio: req.body.bio
        });

        if(typeof req.file !== "undefined") {
          newUser.avatar = '/uploads/userImg/' + req.file.filename;
        } else {
          newUser.avatar = '/uploads/userImg/no-image.png';
        }
        console.log(newUser);
        if(req.body.adminCode === process.env.ADMINCODE) {
          newUser.isAdmin = true;
        }

        if(req.body.answer !== process.env.SECRET){
          req.flash("error", "answer the question");
          return res.redirect("back");
        } else {
          User.register(newUser, req.body.password, function(err, user){
            if(err){
              return res.render("register", {error: err.message});
            }
            passport.authenticate("local")(req, res, function(){
              req.flash("success", "Welcome to Let's Camp " + user.username);
              res.redirect("/campgrounds"); 
            }); 
          });
        }
      });
    });



    var multer = require("multer");
    var storage =   multer.diskStorage({
      destination: function(req, file, callback) {
        callback(null, './public/uploads/userImg');
       },
      filename: function(req, file, callback) {
        callback(null, Date.now() + file.originalname);
      }
    });
    var upload = multer({ storage : storage}).single('image');



<% include ./partials/header %>

    <div class="row">
        <div class="col-xs-8 col-xs-offset-2">
          <form action="/register" method="post">
            <h1 class="text-center">Sign Up</h1>
        </div>
        <div class="row">
          <div class="col-xs-4 col-xs-offset-2">
            <div class="form-group">
              <label for="firstName">First Name</label>
              <input id="firstName" class="form-control" type="text" name="firstName" placeholder="First Name*" required>
            </div>
          </div>
          <div class="col-xs-4 col-xs-offset-0">
            <div class="form-group">
              <label for="lastName">Last Name</label>
              <input id="lastName" class="form-control" type="text" name="lastName" placeholder="Last Name*" required>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-4 col-xs-offset-2">
            <div class="form-group">
              <label for="email">Email</label>
              <input id="email" class="form-control" type="email" name="email" placeholder="Email*" required>
            </div>
          </div>
          <div class="col-xs-4 col-xs-offset-0">
            <div class="form-group">
              <label for="avatar">Avatar Image URL</label>
              <input id="avatar" class="form-control" type="file" name="avatar">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-4 col-xs-offset-2">
            <div class="form-group">
              <label for="username">Username</label>
              <input id="username" class="form-control" type="text" name="username" placeholder="Username*" required>
            </div>
          </div>
          <div class="col-xs-4 col-xs-offset-0">
            <div class="form-group">
              <label for="password">Password</label>
              <input id="password" class="form-control" type="password" name="password" placeholder="Password*" required>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-8 col-xs-offset-2">
            <div class="form-group">
              <label for="bio">Bio</label>
              <textarea id="bio" class="form-control" type="bio" name="bio" rows="5" placeholder="Write a short description of yourself and what you enjoy about camping."></textarea>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-4 col-xs-offset-2">
            <div class="form-group">
              <label for="adminCode">Admin Code</label>
              <input id="adminCode" class="form-control" type="text" name="adminCode" placeholder="Admin Code">
            </div>
          </div>
          <div class="col-xs-4 col-xs-offset-0">
            <div class="form-group">
              <label for="number">Enter: I Love Camping</label>
              <input id="number" class="form-control" type="text" name="answer" placeholder="Answer*" required>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-8 col-xs-offset-2">
            <div class="form-group">
              <button class="btn btn-lg btn-primary btn-block">Sign Up!</button>
            </div>
            <a href="/campgrounds">Go Back</a>
            </form>
          </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <p class="text-center"><strong>*</strong> indicates a required field.</p>
            </div>
        </div>
      </div>

    <% include ./partials/footer %>

如果我使用enctype =“multipart / form-data”我出于某种原因会收到错误但如果我将其关闭则完成但图像仍然无法上传。只需恢复到no-image.png

1 个答案:

答案 0 :(得分:0)

如果您要发布文件您已使用 public void Add(int a, int b) { ... }

entype="multipart/form-data"

如果您遇到其他问题,请解决此问题。

来自documentation

  

。单(字段名)

     

接受名为fieldname的单个文件。单个文件将存储在req.file中。

所以创建这样的上传方法并在你的路由器中调用它:

<form action="/register" method="post" enctype="multipart/form-data">

或重命名输入文件var uploadAvatar = multer({ storage : storage}).single('avatar');

name="image"