Node.js Multer&x2B;反应+;nodejsaxios请求

Node.js Multer&x2B;反应+;nodejsaxios请求,node.js,reactjs,axios,multer,Node.js,Reactjs,Axios,Multer,Axios Post请求 // Create profile export const createProfile = (profileData, avatar, history) => dispatch => { dispatch(clearErrors()); const image = new FormData(); image.append("avatar", avatar, avatar.name); axios .post("/api/profil

Axios Post请求

// Create profile
export const createProfile = (profileData, avatar, history) => dispatch => {
  dispatch(clearErrors());
  const image = new FormData();
  image.append("avatar", avatar, avatar.name);
  axios
    .post("/api/profile", image, profileData)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};
编辑--->Axios post请求第二次尝试

profileData
是我在
req.body
中想要的,而avatar是我在multer后端的
req.file
中收到的,但我收到的是带有图像的
req.body
文件(只是一个空对象)

这是节点中的我的路由器

router.post(
  "/",
  upload.single("avatar"),
  passport.authenticate("jwt", { session: false }),
  (req, res) => {
    console.log(req.body);
      }
    );

<>我在路由文件中将您的路由视为“代码>/API/Prrase<代码> >

您不显示标题
profileData

应该是这样的

const profileData={
标题:{“内容类型”:“多部分/表单数据”}
}


然后,您可以像以前一样向服务器请求。

尝试使用FormData以以下方式实现

handleSubmit(e)
{
  e.preventDefault();
  const err = this.validate();
  if (!err) {
    var formData = {
      category: this.state.category,
      course: this.state.course,
    };
    const { category, course } = this.state;
    let fd = new FormData();
    fd.append('Test', this.state.testFile, this.state.testFile.name);
    fd.append('category', category);
    fd.append('course', course);
    console.log(fd);
    axios({
      method: 'post',
      url: 'http://localhost:7777/api/uploadTest',
      data: fd,
    })
      .then((response) => {
        if (response.data == 'Success') {
          alert('Test has been Added..!!');
        }
        else {
          alert('Something went wrong');
          this.setState({ category: '' });
        }
        // this.setState({success:'Alert: '+response.data});
      })
      .catch((e) => {
        console.error(e);
        this.setState({ success: 'Alert: Something went wrong' });
      });
  }
}

使用axios时,您不必使用axios指定头,并且profileData是函数createProfile随所有配置文件信息一起接收的对象实际上我已经尝试过这样做//创建配置文件导出const createProfile=(profileData,avatar,history)=>dispatch=>{dispatch(clearErrors());const image=new FormData();image.append(“avatar”,avatar,avatar.name);image.append(“user”,profileData,profileData.username);axios.post(“/api/profile”,image)。然后(res=>history.push(“/dashboard”)).catch(err=>dispatch({type:GET_ERRORS,payload:err.response.data});没有结果,我已经编辑了我的答案,因为我不能在这里写代码,请看编辑部分。我没有收到FormData的结果,谢谢!我不知道我可以使用
.append
发送文件中的数据。
handleSubmit(e)
{
  e.preventDefault();
  const err = this.validate();
  if (!err) {
    var formData = {
      category: this.state.category,
      course: this.state.course,
    };
    const { category, course } = this.state;
    let fd = new FormData();
    fd.append('Test', this.state.testFile, this.state.testFile.name);
    fd.append('category', category);
    fd.append('course', course);
    console.log(fd);
    axios({
      method: 'post',
      url: 'http://localhost:7777/api/uploadTest',
      data: fd,
    })
      .then((response) => {
        if (response.data == 'Success') {
          alert('Test has been Added..!!');
        }
        else {
          alert('Something went wrong');
          this.setState({ category: '' });
        }
        // this.setState({success:'Alert: '+response.data});
      })
      .catch((e) => {
        console.error(e);
        this.setState({ success: 'Alert: Something went wrong' });
      });
  }
}