【问题标题】:Multer + React + Nodejs Axios requestMulter + React + Nodejs Axios 请求
【发布时间】:2019-01-06 06:45:49
【问题描述】:

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 发布请求第二次尝试

// Create profile
export 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)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};

profileData 是我在req.body 中想要的,头像是我在后端使用multer 在req.file 中收到的,但我收到的是带有图像的req.file,但我的@ 中没有987654327@(只是一个空对象)

这是我在节点中的路由器

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

【问题讨论】:

    标签: node.js reactjs axios multer


    【解决方案1】:

    尝试使用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' });
          });
      }
    }
    

    【讨论】:

    • 其实我已经尝试过这样做 // 创建配置文件 export const createProfile = (profileData, avatar, history) => dispatch => { dispatch(clearErrors()); const image = new FormData(); image.append("头像", 头像, 头像.name); image.append("用户", profileData, profileData.username); axios .post("/api/profile", image) .then(res => history.push("/dashboard")) .catch(err => dispatch({ type: GET_ERRORS, payload: err.response.data } )); };没有结果
    • 我已经编辑了我的答案,因为我无法在此处编写代码,请查看编辑部分。 formData 没有结果
    • 谢谢!我不知道我可以使用.append 与文件一起发送数据。
    【解决方案2】:

    我认为您的路线在路线文件中为/api/profile

    你没有显示你的标题profileData

    应该是这样的

    const profileData = { headers: { 'content-type': 'multipart/form-data' } }

    然后你就可以像以前那样向服务器请求了。

    【讨论】:

    • 使用 axios,您不必使用 axios 指定标头,而 profileData 是函数 createProfile 接收的包含所有配置文件信息的对象
    猜你喜欢
    • 2022-10-21
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2021-08-29
    • 2020-09-24
    • 1970-01-01
    • 2019-07-24
    相关资源
    最近更新 更多