gpt4 book ai didi

javascript - 无法从 Firebase Storage UploadTask 更新进度栏

转载 作者:行者123 更新时间:2023-11-28 18:25:39 25 4
gpt4 key购买 nike

我正在使用 Angular2 和 Firbase 存储。我试图获取“uploadProgress”值来更新(在 HTML 文件中指定),但不知何故,uploadTask.on 函数中的“uploadProgress”变量可以更新。是因为它在另一个线程上运行吗?这个问题有什么解决方法吗?希望可以有人帮帮我。谢谢

=======================================================================
import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import * as firebase from 'firebase';

@Component({
selector: 'app-create-folder',
templateUrl: './create-folder.component.html'
})

export class CreateFolderComponent {

public fItem: FolderItem;
public f: File;
public storageRef: Storage;
public uploadProgress = 0;


constructor(public af: AngularFire) {}

onSubmit() {

const storageRef = firebase.storage().ref().child('myclient/' +     
this.fItem.file_name);
var uploadTask = storageRef.put(this.f);


uploadTask.on('state_changed', function(snapshot){
this.uploadProgress = (snapshot.bytesTransferred / snapshot.totalBytes) *
100;


});

}


}

最佳答案

Is it because it is running on another thread?

因为它在另一个上下文中运行。

要解决问题,请将 this 分配给“state_changed”回调函数之外的某个变量。之后,您可以使用此变量作为对初始 this 实例的引用。

public uploadFile() {
let self = this;
...
firebaseUploadTask.on('state_changed',
function progress(snapshot) {
self.percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
},
function error(error) {},
function complete() {}
);
}

关于javascript - 无法从 Firebase Storage UploadTask 更新进度栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39222672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com