Unable to send/upload image with axios through formdata in react native

Viewed 50

I've tried following ways but it just does not work while image is being uploaded through postman. What is my mistake?

  export const updateProfile = data => dispatch => {
  const {name, dateOfBirth, gender, customerId, phone, image} = data;
  var bodyFormData = new FormData();    
  bodyFormData.append('image', image);
  bodyFormData.append('name', name);
  bodyFormData.append('dateOfBirth', dateOfBirth);
  bodyFormData.append('gender', gender);
  bodyFormData.append('customerId', customerId);
  bodyFormData.append('phone', phone);
  // bodyFormData.append('file', {
  //   uri: image,
  //   type: 'image/png',
  //   name: 'abc.png',
  // });

  axios({
    url: `${baseUrl}/update-customer`,
    method: 'POST',
    data: bodyFormData,
    headers: {'Content-Type': 'multipart/form-data'},
  })
    .then(function (response) {
      console.log('updateProfile res', response.data);
1 Answers

You need to add type and name with image as well before uploading.

Uncomment your commented code

bodyFormData.append('file', {
   uri: image,
   type: 'multipart/form-data',
   name: 'abc.png',
});

Try once

Related