Axios Multer抛出意外字段错误

时间:2018-10-21 18:00:51

标签: node.js axios multer

所以我已经用邮递员对自己的路线进行了测试,如果我console.log(req.file)得到了以下信息,则会得到以下信息:

{ fieldname: 'myImage',
originalname: '7yFHYXe.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e2 0c 58 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 0c 48 4c 69 6e 6f 02 10 00 00 ... >,
size: 4760663 }

我认为这是正确的。现在,如果我测试axios请求,我将得到MulterError: Unexpected field。这是我的axios代码

...
export const addPostPicture = uploadData => dispatch => {
  var data = new FormData();
  data.append("image", uploadData);
  console.log(data.get("image"));
  axios
    .post("/api/uploads/", data, {
      headers: {
        "Content-Type": `multipart/form-data`
      }
    })
...

当我console.log(data.get("image"));时,我得到以下信息: enter image description here

原谅我的无知,这是我第一次使用multer。因此,任何建议或反馈都很棒! =]

1 个答案:

答案 0 :(得分:0)

就像其他所有错误一样,我很笨,没有相应地使用输入名称字段...

因此更改data.append("myImage", uploadData);解决了我的问题。