MulterError: неожиданное поле - PullRequest
0 голосов
/ 31 октября 2018

route.js

const parser = multer({ storage: storage });

app.post('/api/images', parser.single("MyImage"), function(req, res){
  console.log(req) // to see what is returned to you
  const image = {};
  image.url = req.file.url;
  image.id = req.file.public_id;
  res.send({data:image}) 
});

imageUpload.js или imageUpload.vue или imageUpload.vue.jsx

uploadImage = (event) => {

   let data = new FormData();
   data.append("name", "orderImage");

   data.append("file", event.target.files[0]);

   let config = {
    header: {
      "Content-Type": "image/png"
    }
  };

  axios.post('api/images', data, config).then(response => {
    console.log("image upload response > ", response);
  });
 }

при попытке загрузить изображение в облачное облачное хранилище выдается ошибка.

Ответы [ 2 ]

0 голосов
/ 31 октября 2018

изменить route.js

 const multerName = 'MyImage'

 app.post('/api/images', 
    parser.single('anotherFile'), //here exact change
       function(req, res){

после изменения

app.post('/api/images', parser.single('anotherFile'), function(req, res){

mageUpload.js или imageUpload.vue или imageUpload.vue.jsx

const multerName = 'MyImage'
data.append("name", "orderImage");

здесь также то же имя_мультера, что и у route.js

data.append("file",  //here exact change
   event.target.files[0]);

После изменения

 data.append("multerName", event.target.files[0]);

необходимо поддерживать одинаковые имена как во внешней, так и в внутренней форме formData ()

0 голосов
/ 31 октября 2018

изменить route.js

 const multerName = 'MyImage'
 app.post('/api/images', parser.single(multerName), function(req, res){

mageUpload.js или imageUpload.vue или imageUpload.vue.jsx

const multerName = 'MyImage'
data.append("name", "orderImage");

здесь также то же имя multerName, что и route.js

data.append("multerName", event.target.files[0]);
...