从多个文件上传Firebase存储获取下载url
问题描述:
我是firebase和angularjs中的新成员,我很难从firebase存储获取下载url并将它们存储在firebase实时数据库中。从多个文件上传Firebase存储获取下载url
我能够上传多个文件到firebase存储。问题是当我将下载url存储到firebase实时数据库中时,所有数据库的url值都是相同的。它应该根据每个文件的不同而有所不同。
这里我的脚本:
$scope.submitPhotos = function(file){
console.log(file);
var updateAlbum = [];
for (var i = 0; i < file.length; i++) {
var storageRef=firebase.storage().ref(albumtitle).child(file[i].name);
var task=storageRef.put(file[i]);
task.on('state_changed', function progress(snapshot){
var percentage=(snapshot.bytesTransferred/snapshot.totalBytes)*100;
if (percentage==100){
storageRef.getDownloadURL().then(function(url) {
var galleryRef = firebase.database().ref('gallery/'+albumkey);
var postkey = firebase.database().ref('gallery/'+albumkey).push().key;
updateAlbum={img:url};
firebase.database().ref('gallery/'+ albumkey+'/'+postkey).update(updateAlbum);
});
};
})
};
};
正如你可以看到我能够店的网址到数据库中,但所有的URL都是一样的。我需要的是每个关键存储每个不同的存储链接。
任何帮助赞赏。由于
答
尝试使用下面的代码:
$scope.submitPhotos = function(file){
console.log(file);
var updateAlbum = [];
for (var i = 0; i < file.length; i++) {
var storageRef=firebase.storage().ref(albumtitle).child(file[i].name);
var task=storageRef.put(file[i]);
task.on('state_changed', function progress(snapshot)
{
var percentage=(snapshot.bytesTransferred/snapshot.totalBytes)*100;
// use the percentage as you wish, to show progress of an upload for example
}, // use the function below for error handling
function (error) {
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;
}
}, function complete() //This function executes after a successful upload
{
let dwnURL = task.snapshot.downloadURL;
let galleryRef = firebase.database().ref('gallery/'+albumkey);
let postkey = firebase.database().ref('gallery/'+albumkey).push().key;
updateAlbum={img:dwnURL};
firebase.database().ref('gallery/'+ albumkey+'/'+postkey).update(updateAlbum);
});
};
};
所有最优秀的!
它仍然不起作用。我使用代码获得的结果是firebase数据库仅存储上载到Firebase存储的第一个图像的网址。我需要我的文件存储在firebase数据库中的所有下载url –
嗨,我已经对上面的代码做了一些代码更改;删除了几行,根据需要进行调整。你能确认'文件'数组包含提交多个文件吗? –