1

I'm using "react-image-crop" (https://github.com/DominicTobias/react-image-crop) for cropping an image and then upload it to Firebase storage. The response from "react-image-crop" is a blob:URL in my state. When I call:

var storageRef = firebase.storage().ref();
        var mountainImagesRef = storageRef.child(
            "images/" + auth.uid + "/avatar.jpg"
        );
        mountainImagesRef.put(file).then(function(snapshot) {...

I get the error:

Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File.

I know that I need to pass a blob or file and not the URL but how can I fix this?

Thanks for your help!

2 Answers 2

4

I fixed that, by editing the returned Promise of "react-image-crop". Change return Promise to:

return new Promise((resolve, reject) => {
            canvas.toBlob(blob => {
                if (!blob) {
                    //reject(new Error('Canvas is empty'));
                    console.error("Canvas is empty");
                    return;
                }
                blob.name = fileName;
                console.log(blob);
                resolve(blob);
            }, "image/jpeg");
        });

Now you will get a blob returned and not a blob url.

Sign up to request clarification or add additional context in comments.

1 Comment

where are you getting the fileName from?
2

You can retrieve the blob from an image URL through a fetch request:

async blobFromURL(url) {
 blob = await fetch(this.imageUrl).then(r => r.blob());
 return blob
}

2 Comments

Thanks for your answer. I'm still getting the error. The return is a Promise, can this be the reason for the error?
The fetch request is asynchronous, you need to wait for it to be resolved before uploading it. I never used this plugin but there seems to be a straightforward way to retrieve a blob instead of a url on the documentation: return new Promise((resolve, reject) => { canvas.toBlob(blob => { blob.name = fileName; resolve(blob); }, 'image/jpeg', 1); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.