2

I try to upload the image into Firebase. I am using 'react-native-image-picker' but every time i am getting error. Firebase Storage: String does not match format 'base64': Invalid character found. I saw many post Related to this issue but none is working.

uploadImage = response = >
{
    let metadata = {
        contentType: response.type
    }
    let randomId = shortid.generate()
    let fileExtension
    if (Platform.OS == "android") fileExtension = response.type.split("/")[1]
    else
    {
        fileExtension = response.fileName.split(".")[1]
    }
    let storagePath =
        "/Base_Url/" +
        this.props.username +
        "/" +
        this.props.sessionId +
        "/" +
        randomId +
        "." +
        fileExtension
    let storingRef = firebase.storage()
        .ref(storagePath);
    storingRef.putString(response.data, "base64url", metadata)
        .on(
            firebase.storage.TaskEvent.STATE_CHANGED,
            snapshot = >
            {
                var progress =
                    snapshot.bytesTransferred / snapshot.totalBytes * 100;
                if (progress === 100)
                {
                    let downloadURL = FireBaseUtils.getDownloadURL(storagePath);
                    this.setState(
                    {
                        url: snapshot.downloadURL
                    });
                }
                else
                {
                    switch (snapshot.state)
                    {
                    case firebase.storage.TaskState.PAUSED:
                        break;
                    case firebase.storage.TaskState.RUNNING:
                        break;
                    }
                }
            },
            error = >
            {
                switch (error.code)
                {
                case "storage/unauthorized":
                    break;
                case "storage/canceled":
                    break;
                case "storage/unknown":
                    break;
                }
            }
    );
};
Dheeraj Kumar
  • 119
  • 2
  • 12

1 Answers1

0

Try to use putString(message, 'base64') (not base64url)

If a Blob, File, or Uint8Array isn't available, you can use the putString() method to upload a raw, base64, base64url, or data_url encoded string to Cloud Storage.

https://firebase.google.com/docs/storage/web/upload-files

Hope it helps

soutot
  • 3,531
  • 1
  • 18
  • 22
  • 1
    i tried putString(message, 'base64') but it also not working – Dheeraj Kumar Sep 25 '17 at 04:44
  • Can you try adding `substring(23)`, please? Something like `toringRef.putString(response.data.substring(23), "base64")` let me know if it helps – soutot Sep 26 '17 at 22:27
  • @DheerajKumar Did you find a solution? – Yossi Nov 13 '18 at 16:44
  • @Yossi I ended up facing a similar issue and posted a solution here: https://stackoverflow.com/questions/52157253/firebase-storage-string-does-not-match-format-base64-invalid-character-found basically JavaScript core doesn't handle atob/bota methods to decode/encode base64 so we have to add it by ourselves. Hope it can solve your issue – soutot Nov 13 '18 at 17:42