反应原生 base64 图像上传失败,状态码为 400

时间:2021-05-27 06:14:11

标签: reactjs react-native axios

我想将我的图像作为 base 64 上传到服务器,在用户选择图像后,它存储为 base64 状态,然后我调用上传函数,但它给了我这个错误: <Column field="0" header="Vin" /> <Column field="1" header="Year" /> <Column field="2" header="Brand" /> <Column field="3" header="Color" /> 。 我需要先调用一个 API,它需要用户 id 并以上传名称 id 响应,然后我调用上传图像 API

这是我的实现:

request failed with status code 400

谁能帮我解决这个问题? ty

1 个答案:

答案 0 :(得分:0)

您需要在标题中添加 contentType

'Content-Type': `multipart/form-data;`,

这是一个例子

npm install --save form-data

import FormData from 'form-data'

let data = new FormData();
data.append('file', file, file.name);

return (dispatch) => {
axios.post(URL, data, {
  headers: {
    'accept': 'application/json',
    'Accept-Language': 'en-US,en;q=0.8',
    'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
  }
})
  .then((response) => {
    //handle success
  }).catch((error) => {
    //handle error
  });
};}