使用带有saga的axios将文件从formik reactjs发送到springboot

问题描述 投票:0回答:1

我可以从邮递员发送文件“ userPicture”并在后端(springboot)中获取它,然后将它保存到数据库mongodb中而没有任何问题但是当我从表单发送图片时,我在reactjs和axios中使用Formik组件我显示输入用户的内容我得到的图片enter image description here

在axios中,我这样做是为了发送请求。对于边界,我不知道从示例中复制过去的“ WebKitFormBoundaryQ0pBuvRC1EzDAQWT”是怎么做的

  try {
        const { user } = action;
        console.log(user);
        const request = yield axios({
            method: 'post',
            url: ENDPOINTS.USER + '/add',
            data: user,
            headers: { 'Content-Type': 'multipart/form-data;boundary=----WebKitFormBoundaryQ0pBuvRC1EzDAQWT----' }
        });

在后端,我收到此错误

,org.springframework.validation.BindingResult,org.springframework.web.multipart.MultipartFile,org.springframework.web.multipart.MultipartFile)引发java.io.IOException:必需的请求部分'userPicture'不存在

当我发送不带userPicture参数的请求时,邮递员出现了相同的错误

reactjs axios redux-saga formik saga
1个回答
0
投票

看起来您发送文件不正确。如果要将multipart/form-data从客户端发送到服务器,则需要使用FormData

喜欢此

const formData = new FormData();
formData.append('usePicture', user)
© www.soinside.com 2019 - 2024. All rights reserved.