我想要一个本机的动态进度栏。我正在使用Firebase存储。问题是,我无法跟踪上载图像的进度。当我进行控制台操作时,它显示0和1。0到1之间没有值。因此,它在闪烁中显示进度条。我还使用了Observables和setInterval进行跟踪,但是没有用。我无法更新firebase.storage.TaskState.RUNNING中的状态。
我关注了以下链接:
但对我没有任何帮助。
本机
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 !!! ");
}
);
~~~