错误:意外字段Amazon S3,Express,Multer,MulterS3

时间:2017-07-26 07:45:46

标签: express aws-sdk multer multer-s3

我收到此错误,我不知道为什么它给我这个错误,我检查了所有与此相关的教程/类似问题,没有任何工作。有人可以帮我指点正确的方向。

谢谢,

错误:

意外字段

错误:意外字段
    at makeError(C:\ Users \ Administrator \ Desktop \ express-cc-master \ node_modules \ multer \ lib \ make-error.js:12:13)     at wrappedFileFilter(C:\ Users \ Administrator \ Desktop \ express-cc-master \ node_modules \ multer \ index.js:40:19)

Profile.js



var aws = require('aws-sdk')
var express = require('express');
var router = express.Router();
var multer = require('multer')
var multerS3 = require('multer-s3')

 
aws.config.loadFromPath('./config.json');
aws.config.update({
    signatureVersion: 'v4'
});
 
var s3 = new aws.S3({});

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'raytestbucket123',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now()+file.originalname)
    }
  })
})

router.get('/profile', function(req, res){
  res.render('profile', { title: 'Profile' });

});


router.post('/profile', upload.single('file'), function(req, res, next){

  res.send(req.files);
  console.log(req.files);

})


module.exports = router;




Profile.hbs



{{> header }}



  <body> 
    <form action="/profile" method="POST" enctype="multipart/form-data">
      File to upload to S3: 
      <input name="file" type="file"> 
      <br> 
      <input type="submit" value="Upload File to S3"> 
    </form> 
  </body>

<div class="container">
<h2>Profile</h2>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我自己发现了这个问题。

而不是

res.send(req.files);   的console.log(req.files);

应该是

res.send(req.file);   的console.log(req.file);