Ikiugu
Ikiugu

Reputation: 677

Upload image using fetch doesn't work in React Native (iOS)

I am working on a mobile app using React Native and posting an image on iOS doesn't work. I have hooked up my code to requestbin, setup the info.plist to allow non-https urls and other post requests are working (e.g login). For the image, all I get is a blank body for the request. Here is the code posting the image:

uploadImage = () => {
const data = new FormData();
data.append('photo', {
  uri: this.state.logo.uri,
  name: 'logo'
});
fetch([requestbin url here], {
  method: 'post',
  body: data
}).then(res => {
  console.log(res);
});

for the image, I am using react-native-image-picker to get it and store it in state under the variable 'logo'. Here is that code as well

 handleNewImage = () => {
var options = {
  title: 'Choose new company logo',
  storageOptions: {
    skipBackup: true,
    path: 'images'
  }
};

showImagePicker(options, response => {
  console.log('Response = ', response);

  if (response.didCancel) {
    console.log('User cancelled image picker');
  } else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  } else if (response.customButton) {
    console.log('User tapped custom button: ', response.customButton);
  } else {
    let source = { uri: response.uri };

    // You can also display the image using data:
    // let source = { uri: 'data:image/jpeg;base64,' + response.data };

    this.setState({
      logo: source
    });
  }
});

Upvotes: 0

Views: 3197

Answers (3)

Ehsan Amiri
Ehsan Amiri

Reputation: 131

Remember that you also should pass a name key too, like below:

    let url     = "",
        headers = "",
        method  = "POST",
        body    = new FormData(),
        uri     = "URI of the picked image.";

    body.append("photo", {
            name: "Just a name",
            uri : Platform.OS === "android" ? uri : uri.replace("file://", "")
        }
    );

    fetch(url, method, headers, body)
        .then(function (response) {
        })
        .catch(function (error) {
        });

Upvotes: 4

Chandra Pal
Chandra Pal

Reputation: 26

function uploadProfilePicture(mImage) {

    var data = new FormData();
    data.append('theFile', { uri: mImage.uri, name: 'profile_photo.jpg', type: 'image/jpg' });

    fetch(AppConstant.BASE_URL + AppConstant.upload_media, {
        method: 'POST',
        body: data
    })
        .then((response) => response.json())
        .then((responseJson) => {

            var err = 'error_message' in responseJson ? true : false
            if (err) {
                alert(responseJson.error_message)

            } else {


                alert(JSON.stringify(responseJson))

            }

        })
        .catch((error) => {
            console.error(error);
            alert(error)
        });

}

Upvotes: 1

Ikiugu
Ikiugu

Reputation: 677

If anyone has issues with using fetch in iOS, check out react-native-file-upload I have found it to be extremely helpful in both image uploading and regular posts.

Upvotes: -1

Related Questions