Node.js 邮递员可以将文件上载到节点api。但不是用反应
重复编码Node.js 邮递员可以将文件上载到节点api。但不是用反应,node.js,reactjs,react-redux,axios,mern,Node.js,Reactjs,React Redux,Axios,Mern,重复编码 export const uploadImage = data => async dispatch => { const config = { headers: { "Content-Type": "multipart/form-data" } }; try { const res = await axios.post("/api/profile/upload", data,
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", data, {
// 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为空
我试图上传文件与邮递员相同的配置,它似乎api工程,但不能上传与反应formdata
任何想法都将不胜感激。提前感谢尝试这样做:
const formData = new FormData();
formData.append('file', file);
const res = await axios.post("/api/profile/upload", formData, config);
如果在
uploadImage
函数中执行console.log(data)
操作,是否会获取数据?是的,我在uploadImage函数中获取FormData对象。我还尝试直接从react组件发出请求,但它也无法正常工作。不知何故,axios无法发布FormData。