Firebase Storage: String does not match 'base64' format: invalid character detected

I am working on native-native and I need to upload an image to Firebase using Firebase Storage . I use react-native-image-picker to select a phone image that gives me base64 encoded data.

When I try to upload the image to Firebase, it gives me an error Firebase Storage: String does not match format 'base64': Invalid character found, but I already checked if the string is a valid base64 string with a regular expression, and that is!

I already read a few answers from here, but I tried all this. Here is my code:

function uploadImage(image){
  const user = getCurrentUser();
  const refImage = app.storage().ref(`profileImages/${user.uid}`);

  refImage.putString(image, 'base64').then(() => {
    console.log('Image uploaded');
  });
}

Image Selection:

ImagePicker.showImagePicker(ImageOptions, (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 {
    uploadImage(response.data);
  }
});
+4
3

"data:image/jpeg;base64,. only data

+4

image - URL- base64, data_url:

function uploadImage(image){
  const user = getCurrentUser();
  const refImage = app.storage().ref(`profileImages/${user.uid}`);

  refImage.putString(image, 'data_url', {contentType:’image/jpg’}).then(() => {
    console.log('Image uploaded');
  });
}
+4

image.substring (23)

function uploadImage(image){
      const user = getCurrentUser();
      const refImage = app.storage().ref(`profileImages/${user.uid}`);

      refImage.putString(image.substring(23), 'base64').then(() => {
        console.log('Image uploaded');
      });
    }
+3
source

Source: https://habr.com/ru/post/1664659/


All Articles