如何检测特定的Firebase存储路径数据是否已更改 [英] How do I detect if a particular firebase storage path data has changed
问题描述
firebase数据库具有一项功能,当路径发生更改时,我可以再次检索数据
firebase database has a function where I can retrieve data again when something changed in the path
firebase.database().ref('users/' + Auth.uid + '/profileImg').on('value', function(snapshot) { //do things when the data changed});
我想知道是否有人知道Firebase存储是否做同样的事情?例如,如果我上传另一个个人资料照片,如何获取该imageUrl?
I was wondering if anyone know if firebase storage does the same thing? For example if I upload another profile pic, how can I can retrieve that imageUrl?
我知道我可以通过以下方式检索它,但是由于我想检测与上载位置不在同一控制器中的另一个控制器中的更改,因此该方法不起作用.
I understand I can retrieve it in the way below, but since I want to detect changes in another controller that is not in the same controller as the upload place, this method would not work.
uploadTask.on('state_changed', function (snapshot) {
// Observe state change events such as progress, pause, and resume
// See below for more detail
}, function (error) {
// Handle unsuccessful uploads
}, function () {
$scope.userProfile = uploadTask.snapshot.downloadURL;
});
}, function (error) {
console.error(error);
});
谢谢!
推荐答案
Firebase Storage不具有内置功能,可在文件更改时主动向客户端发出警报.
Firebase Storage has no built-in capability to proactively alert clients when a file is changed.
通过将Firebase存储与Firebase实时数据库结合起来,可以轻松构建此内容.将文件的downloadURL保留在数据库中,并添加一个lastModified
时间戳:
This is something you can easily build by combining Firebase Storage with the Firebase Realtime Database. Keep the downloadURL of your file in the database and add a lastModified
timestamp:
images
$imageid
downloadUrl: "https://downloadUrl"
lastModified: 123873278327
每当您在Firebase Storage中上载/更新映像时,请更新数据库中的downloadUrl/时间戳:
Whenever you upload/update an image in Firebase Storage, update the downloadUrl/timestamp in the database:
uploadTask.on('state_changed', function (snapshot) {
// Observe state change events such as progress, pause, and resume
// See below for more detail
}, function (error) {
// Handle unsuccessful uploads
}, function () {
$scope.userProfile = uploadTask.snapshot.downloadURL;
databaseRef.child('images').child(imageId).set({
downloadUrl: uploadTask.snapshot.downloadURL,
lastModified: firebase.database.ServerValue.TIMESTAMP
})
});
现在,通过侦听该图像的数据库位置,您可以知道该图像何时被修改:
Now you can know when an image has been modified, by listening to the database location for that image:
databaseRef.child('images').child(imageId).on('value', function(snapshot) {
// take the downloadUrl from the snapshot and update the UI
});
这篇关于如何检测特定的Firebase存储路径数据是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!