1

我正在尝试使用 React Native axios 上传图片。但我得到了这个回应。我尝试了所有解决方案,但没有奏效。我正在使用 react-native-image-picker 来获取图像

{ result: null,
  message: 'Wrong access',
  error: true,
  type: 'command_not_found' }

这是我的代码

    ImagePicker.showImagePicker(options, (response) => {

        let formData = new FormData();
        formData.append('image', { uri: response.uri, name: response.fileName, type:response.type });
        let config = {
           headers: {
                   'Content-Type': 'multipart/form-data'
           }
        }
        axios({
           url: "URL",
           method: 'POST',
           data: formData,
           config
         })
         .then(result => console.log(result))
         .catch(error => console.log(error))

     }
4

1 回答 1

0

尝试使用原始fetchapi。

const createFormData = (photo) => {
  const data = new FormData();

  data.append("photo", {
    name: photo.fileName,
    type: photo.type,
    uri:
      Platform.OS === "android" ? photo.uri : photo.uri.replace("file://", "")
  });

  return data;
};

然后尝试再次上传

fetch("http://localhost:3000/api/upload", {
    method: "POST",
    body: createFormData(photo)
});
于 2019-10-18T09:53:14.833 回答