Javascript 如何在react native中使用axios将图像上载到服务器?

Javascript 如何在react native中使用axios将图像上载到服务器?,javascript,reactjs,react-native,axios,Javascript,Reactjs,React Native,Axios,我想用react native将一个图像文件上传到服务器上,我该怎么做? 这是我的密码: 在我的index.js入口点中,我设置了axios默认配置: axios.defaults.baseURL = BaseUrl; axios.defaults.headers.common['Content-Type'] = 'application/json'; axios.defaults.headers.common['Accept'] = 'application/json'; 现在在我的prof

我想用react native将一个图像文件上传到服务器上,我该怎么做? 这是我的密码:

在我的
index.js
入口点中,我设置了axios默认配置:

axios.defaults.baseURL = BaseUrl;
axios.defaults.headers.common['Content-Type'] = 'application/json';
axios.defaults.headers.common['Accept'] = 'application/json';
现在在我的
profileEdit.js
中,我需要上传一个配置文件图像

  let data = new FormData();
  data.append('profile_picture',
      {uri: imageResponse.uri, name: imageResponse.fileName, type: 'image/jpg'});

// imageResponse is a response object that I m getting from React native  ImagePicker 

    axios.post('profile/picture/', data,
       {
          headers: {
                    "Authorization": "JWT " + this.state.token,
                    'content-type': 'multipart/form-data',
                  }
        }).then(response => {
                console.log('Profile picture uploaded successfully', response);

        }).catch(error => {
              console.log('Failed to upload profile image', error);
              console.log('Error response',error.response);

        });
但这给了我网络错误,而其他API工作正常

我遵循这里给出的解决方案

这是我得到的错误响应

我的请求头是这样的

我不想使用任何其他软件包,例如

更多我关注的链接:


谁能告诉我哪里做错了,我该如何处理这个问题。ty

我认为您的Axios POST请求标题可能不正确,因为您正在使用
FormData()
构造函数存储图像数据:

您可能需要尝试以下操作:

let data=new FormData();
data.append('profile_picture'{
uri:imageResponse.uri,
名称:imageResponse.fileName,
类型:'image/jpg'}
);
axios.post('profile/picture/',数据,
{
标题:{
“内容类型”:“多部分/表单数据;边界=${data.\u boundary}`,
…data.getHeaders()
}
}
)

只需尝试更改“内容类型”:“多部分/表单数据”。。。。它会起作用的,我从