Image upload with react native axios

107 views Asked by At

I am trying to upload image via axios to an API but I have issues the server sends no response at all no error this is the code

image picker

const showImage = () => {
ImagePicker.showImagePicker(options, (response) => {
  if (response.didCancel) {
    setIsImageEdit(false);
  } else if (response.error) {
  } else if (response.customButton) {
  } else {
    const source = response;
    setUserImage({
      fileLink: source.uri,
    });
    setUploadImageFile(source.uri);
  }
});

};

and code for upload via axios

setActivity(true);
const token = await getToken();
if (uploadImageFile) {
  const uploadUri =
    Platform.OS === 'ios'
      ? uploadImageFile.replace('file://', '')
      : uploadImageFile;

  const data = new FormData();
  data.append('file', {
    uri: uploadImageFile,
    name: 'file',
    type: 'image/jpg',
  });
  console.log(uploadImageFile);
  console.log(data);
  Axios.post('http://capi.beebl.io/user/image', data, {
    headers: {
      Authorization: token,
      'Content-Type': 'multipart/form-data',
    },
  })
    .then((res) => {
      console.log(res);
      setActivity(false);
    })
    .catch((err) => {
      console.log(err);
      setActivity(false);
    });

there is no error displayed the activity indicator goes on and one and no response

I am uploading the image via postman and everything is good it uploads

1

There are 1 answers

0
Batraz Jioty On BEST ANSWER

try instead of uploadImageFile.replace('file://', '') this: uploadImageFile.replace('file:', '')