React Native RNFetchBlob multipart request builder发现一个没有“data”属性的字段,将隐式删除该字段“null”

pbossiut  于 2023-10-22  发布在  React
关注(0)|答案(1)|浏览(115)
const formData = new FormData();

for (let i = 0; i < image.length; i++) {
  formData.append("postFile", image[i].data);

}

formData.append("imagesList", null);

console.log("formData..", formData)

rnfetchblob.fetch('POST', `${baseUrl}/fame/saveImage`, {
  Authorization: `Bearer ${token}`,
  otherHeader: "foo",
  'Content-Type': 'multipart/form-data',
}, [
  formData,
]).then((resp) => {
  console.log("response = ", resp)
}).catch((err) => {
  console.log("error = ", err)
})

我已经尝试了太多次,但数据是空的,我甚至花了一整天的时间在react原生获取blob文档上。

t3psigkw

t3psigkw1#

尝试使用此函数uploadImage
我对这个函数做了什么修改,我改变了负载,使用fetch代替rnfetchblob。fetch

let uploadImage = async () => {
  const formData = new FormData();
  formData.append('imagesList', null)
  image.forEach((file: any) => {
    formData.append('postFile', {
      name: file?.fileName,
      size: file?.fileSize,
      type: file?.type,
      uri: file?.uri
    });
  });
    let res = await fetch(
      `${baseUrl}/fame/saveImage`,
      {
        method: 'post',
        body: formData,
        headers: {
          Authorization: `Bearer ${token}`,
          'Content-Type': 'multipart/form-data',
        },
      }
    );
    let responseJson = await res.json();
    if (responseJson.status == 1) {
      alert('Upload Successful');
    }
};

相关问题