鍍金池/ 問答/HTML/ Error: Unexpected field

Error: Unexpected field

html

<form action="http://localhost:8085/api/uploadimg" method="POST" enctype="multipart/form-data" target="nm_iframe">
  <input @change="getImgFile" type="file" name="imgfile">
  <button class="btn btn-default" type="button" @click="publish"><span>提交</span></button>
</form>
<iframe name="nm_iframe" style="display:none;"></iframe>

js

getImgFile (event) {
  let ele = event.srcElement ? event.srcElement : event.target;
  this.img = ele.files[0];
  console.log(ele.files[0]);
},
publish () {
  if (this.img) {
    let formData = new FormData();
    formData.append('file', this.img);
    let config = {
      headers: {'Content-Type': 'multipart/form-data'}
    }
    this.$http.post(`/api/uploadimg`, formData, config).then(response => {
      console.log(response);
    }).catch(function(error) {
      console.log(error)
    })
  } else {
    alert('請選擇文件!');
  }
},

node

// upload.js
var multer = require('multer');
var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, './uploads')
    },
    filename: function(req, file, cb) {
        var fileFormat = (file.originalname).split(".");
        cb(null, file.fieldname + '-' + Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
});
var upload = multer({
    storage: storage
});
module.exports = upload;
// 圖片上傳
var multer = require("./upload");
app.post('/uploadimg', multer.single('imgfile'), function(req, res, next) {
    var files = req.files;
    console.log(files);
    if (!files[0]) {
        outPut(res, JSON.stringify(util.mergeObj(error, { message: '上傳文件錯誤!' })))
    } else {
        outPut(res, JSON.stringify(util.mergeObj(success, files[0])))
    }
})

我在前端上傳圖片時,報錯500,提示信息Error: Unexpected field,網(wǎng)上找答案,都是name不一致的說法,但是我的name都是imgfile,求大神告知原因。

回答
編輯回答
撥弦

不知你的問題是否解決了, 前端報500的同時,你的nodejs server端應(yīng)該有相應(yīng)的錯誤日志,看看這個Error報錯發(fā)生的具體文件和行號 有助于進(jìn)一步分析你的報錯原因。
如果還沒解決,可以留言給我?guī)湍憧聪隆?/p>

2017年12月1日 15:06