简体   繁体   English

上载的图片不适用于multer

[英]Uploaded image not working with multer

I am trying to upload a picture with express and multer. 我正在尝试上传带有快递和穆勒的图片。 Here is the form I use to upload the image: 这是我用来上传图片的表格:

<form action="/uploadProfilePicture" method="POST" id="form-upload-profile-picture">
  <div class="row center">
      <div class="row">
          <input type="file" name="uploadProfilePicture" id="add-friend-search-input">
      </div>
      <div class="row">
          <button class="button-green-large" class="u-full-width" id="add-friend-search-submit">Upload</button>
      </div>
  </form>

And here is my back end: 这是我的后端:

var mkdirp           = require("mkdirp"),
    multer           = require("multer");

// MULTER CONFIGURATION
    var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        //var code = JSON.parse(req.body.model).empCode;
        var dest = "/public/images";
        mkdirp(dest, function (err) {
            console.log(dest);
            if (err) cb(err, dest);
            else cb(null, dest);
        });
    },
    filename: function (req, file, cb) {
        cb(null, Date.now()+'-'+file.originalname);
    }
});

// POST ROUTE: Upload profile picture
app.post("/uploadProfilePicture", upload.any(), function(req, res) {
    console.log(req.body);
    res.send(req.files);
});

However, whenever I upload an image, it does not appear in /public/images. 但是,每当我上传图像时,它都不会出现在/ public / images中。 What is wrong with my code? 我的代码有什么问题? Thanks so much! 非常感谢!

enctype="multipart/form-data" is missing in your html form tag. 您的html表单标签中缺少enctype="multipart/form-data" Without that, it will post your form as application/x-www-form-urlencoded, which will not include any file data. 否则,它将把您的表单发布为application / x-www-form-urlencoded,其中将不包含任何文件数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM