一旦在Firebase存储上完成上传,我如何获得下载URL [英] How do i get download URL once upload is done on firebase storage
问题描述
图像上传完成后,我想获取下载URL,然后将downloadURL设置为我的newAds.picLink(object.property),但是在此代码中,因为正在进行上传
I want to get the download URL once image upload is finished and then the downloadURL is set to my newAds.picLink(object.property) , but in this code since upload is in progress
newAds.update({ picLink: downloadURL });
调用
会引发错误,因为downloadURL当前不可用并且正在进行中.我已经通过使用setTimeOut来解决此问题,设置为8秒钟,这可以使图像首先完全上传,然后接收到downloadURL,但这是不正确的.
is called which throws an error since downloadURL is currently not available and under progress. I have resolved this by using setTimeOut to 8 sec which lets the image to upload completely first and then downloadURL is recieved but that's not correct.
一旦图像完全上传,我如何创建一个正确的回调来设置newAds.picLink = downloadURL?
How can i create a correct callback that sets newAds.picLink = downloadURL once image is completely uploaded?
addSubmitted.addEventListener('click', e => {
const newAds = _db.ref(userID).push();
const newAd = {
};
const ref = firebase.storage().ref();
const file = $('#exampleInputFile').get(0).files[0];
const name = (+new Date() + '-' + file.name);
const task = ref.child(name).put(file, {contentType: file.type});
function abc() {
task.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log('File available at', downloadURL);
console.log(downloadURL);
newAds.update({ picLink: downloadURL });
});
task.catch(error => {
// Use to signal error if something goes wrong.
console.log(`Failed to upload file and get link - ${error}`);
});
}
setTimeout(abc,8000);