React Native中的Firebase动态进度栏

时间:2019-07-12 20:17:56

标签: javascript android reactjs firebase react-native

我想要一个本机的动态进度栏。我正在使用Firebase存储。问题是,我无法跟踪上载图像的进度。当我进行控制台操作时,它显示0和1。0到1之间没有值。因此,它在闪烁中显示进度条。我还使用了Observables和setInterval进行跟踪,但是没有用。我无法更新firebase.storage.TaskState.RUNNING中的状态。

我关注了以下链接:

  1. https://forum.ionicframework.com/t/progress-bar-for-firebase-upload/86627

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

  3. https://medium.com/backticks-tildes/react-native-image-upload-to-firebase-cloud-storage-ios-24b8dc0a9c8b

但对我没有任何帮助。

本机

 firebase.storage()
            .ref(`licence_pic/images/${filename}`)
            .putFile(this.state.filePath.uri)
            .on(
                firebase.storage.TaskEvent.STATE_CHANGED,
                snapshot => {
                    let state = {};
                    state = {
                        ...state,
                        progress: (snapshot.bytesTransferred / snapshot.totalBytes) * 100 // Calculate progress percentage
                    };

                    // const interval = new Observable(observer => {
                    //     const interval = setInterval(() => {
                    //         observer.next((snapshot.bytesTransferred));
                    //     }, 100);

                    //     // once we stop listening to values clear the interval
                    //     return () => {
                    //         clearInterval(interval);
                    //     }
                    // });

                    // interval.subscribe(value => console.log(value));

                    console.log(snapshot.bytesTransferred);

                    if (snapshot.state == firebase.storage.TaskState.RUNNING) {
                        console.log("Running")
                    }

                    if (snapshot.state === firebase.storage.TaskState.SUCCESS) {
                        console.log("Success")
                        this.setState({uploadSuccess:true})
                        // interval.unsubscribe();
                        // Observable.unsubscribe();
                    }
                    this.setState(state);
                },
                error => {
                    //   Observable.unsubscribe();
                    //   interval.unsubscribe();
                    alert('Sorry, Try again.');
                }
            );

用Java语言

         task.on('state_changed',
             function progress(snapshot){
                 if (snapshot.state == firebase.storage.TaskState.RUNNING) {
                     console.log((snapshot.bytesTransferred / snapshot.totalBytes) * 100);            
                 }

                switch (snapshot.state) {
                     case firebase.storage.TaskState.SUCCESS:
                     console.log('Upload is complete');
                     break;
                     case firebase.storage.TaskState.RUNNING:
                     console.log('Upload is running');
                     break;
                     default:
                     console.log(snapshot.state);
                 }

             },
             function error(err){
                 console.log(err)

             },
             function complete(){
                 console.log("file uploaded !!! ");
             }
         );

~~~

0 个答案:

没有答案