一旦在Firebase存储上完成上传,我如何获得下载URL [英] How do i get download URL once upload is done on firebase storage

查看:65
本文介绍了一旦在Firebase存储上完成上传,我如何获得下载URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图像上传完成后,我想获取下载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);

推荐答案

来自

var uploadTask = storageRef.child('images/rivers.jpg').put(file);

// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
  // Observe state change events such as progress, pause, and resume
  // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
  var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  console.log('Upload is ' + progress + '% done');
  switch (snapshot.state) {
    case firebase.storage.TaskState.PAUSED: // or 'paused'
      console.log('Upload is paused');
      break;
    case firebase.storage.TaskState.RUNNING: // or 'running'
      console.log('Upload is running');
      break;
  }
}, function(error) {
  // Handle unsuccessful uploads
}, function() {
  // Handle successful uploads on complete
  // For instance, get the download URL: https://firebasestorage.googleapis.com/...
  uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
    console.log('File available at', downloadURL);
  });
});

因此,这注册了一个state_changed观察者.上传完成后,这会调用StorageReference.getDownloadURL()获取下载URL.

So this registers a state_changed observer. When the upload has completed, this calls StorageReference.getDownloadURL() to get the download URL.

这篇关于一旦在Firebase存储上完成上传,我如何获得下载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆