邮递员可以将文件上传到Node api。但是没有React

Redux代码

export const uploadImage = data => async dispatch => {
    const config = {
        headers: {
            "Content-Type": "multipart/form-data"
        }
    };

    try {
        const res = await axios.post("/api/profile/upload",data,config);
        // const res = await axios.post("/api/profile/upload",{
        //  headers: {
        //      "Content-Type": "multipart/form-data"
        //  }
        // });
        console.log(res);
        dispatch({
            type: AVATAR_UPLOAD,payload: res.data
        });
    } catch (err) {
        const errors = err.response.data.errors;

        if (errors) {
            errors.forEach(error => dispatch(setalert(error.msg,"danger")));
        }

        dispatch({
            type: PROFILE_ERROR,payload: { msg: err.response.statusText,status: err.response.status }
        });
    }
};

反应代码

const data = new FormData();
data.append("file",avatar);
uploadImage(data);

我收到错误的请求消息,req.files为空 我尝试使用Postman相同的配置上传文件,看来api可以使用,但是无法使用react formdata进行上传。

任何想法将不胜感激。预先感谢

yndf345678 回答:邮递员可以将文件上传到Node api。但是没有React

尝试这样做:

const formData = new FormData();
formData.append('file',file);
const res = await axios.post("/api/profile/upload",formData,config);
本文链接:https://www.f2er.com/3099927.html

大家都在问