Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This method breaks my react-native app #383

Open
co-de opened this issue Jun 20, 2019 · 0 comments
Open

This method breaks my react-native app #383

co-de opened this issue Jun 20, 2019 · 0 comments

Comments

@co-de
Copy link

co-de commented Jun 20, 2019

Here is a link to my SO thread about the issue-> https://stackoverflow.com/questions/56660861/this-method-seems-to-break-my-react-native-code
The below code seems to break my app. I suspect it is related to RNFetchBlob.
This has been blocking me for days. Can you help me with this please?

Edit: I believe this could be related: wkh237#69

Thanks

uploadImage(uri, mime, name) {
        console.log("inside uploadImage function");
        try{
            const Blob = RNFetchBlob.polyfill.Blob;
            const fs = RNFetchBlob.fs;
            window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
            window.Blob = Blob;  
            var imgUri = uri; 
            var uploadBlob = null;
            const uploadUri = Platform.OS === 'ios' ? imgUri.replace('file://', '') : imgUri;
            var imageKey = (this.state.currentImageIndex).toString();//this.guidGenerator();
            var imageRef = firebase.storage().ref(`/images/${imageKey}`);

            fs.readFile(uploadUri, 'base64')
            .then(data => {
                return Blob.build(data, { type: `${mime};BASE64` });
            })
            .then(blob => {
                uploadBlob = blob;
                var uploadTask = imageRef.put(blob, { contentType: mime, name: name });
                uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
                    (snapshot) => {
                        // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
                        var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                        console.log('Upload is ' + progress + '% done');
                        switch (snapshot.state) {
                        case firebase.storage.TaskState.PAUSED: // or 'paused'
                            console.log('Upload is paused');
                            break;
                        case firebase.storage.TaskState.RUNNING: // or 'running'
                            console.log('Upload is running');
                            break;
                        }
                    }, (error) => {
                        console.log(error);
                    // A full list of error codes is available at
                    // https://firebase.google.com/docs/storage/web/handle-errors
                    switch (error.code) {
                        case 'storage/unauthorized':
                        // User doesn't have permission to access the object
                        break;

                        case 'storage/canceled':
                        // User canceled the upload
                        break;

                        case 'storage/unknown':
                        // Unknown error occurred, inspect error.serverResponse
                        break;
                    }
                    }, () => {
                        // Upload completed successfully, now we can get the download URL
                        uploadTask.snapshot.ref.getDownloadURL().then((downloadURL)=> {
                            try{
                                console.log('File available at', downloadURL);

                                uploadBlob.close();
                                fs.unlink(uploadUri);
                                console.log("blob closed");

                                this.setState({currentImageIndex:this.state.currentImageIndex+1}, ()=>{
                                    if(this.state.pickType ==='multi'){
                                        if(this.state.currentImageIndex<this.state.totalNumImages){
                                            console.log("call openCropper");
                                            this.openCropper();
                                        }else{
                                            this.setState({totalNumImages:0});
                                            this.setState({currentImageIndex:0});
                                        }
                                    }
                                });
                            }catch(err){
                                console.log(err);
                            }
                        }).catch((err)=>{
                            console.log(err);
                        });
                    });
            })
            .catch((err)=>{
                console.log("ERROR READING FILE: fs.readFile!!");
                console.log(err);
            });
        }
        catch(err){
            console.log(err);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant