Firebase成功上传到firebase存储后获取下载URL

Bri*_*vie 12 javascript firebase firebase-realtime-database firebase-storage angular

我正在尝试将单个图像上传到Firebase存储,然后获取其下载URL并将其分配给变量.

我可以成功将我的图像上传到firebase,但是我无法检索下载网址.这是我已经尝试过的.

upload() {
    let storageRef = firebase.storage().ref();
    let success = false;

    for (let selectedFile of [(<HTMLInputElement>document.getElementById('file')).files[0]]) {
      let router = this.router;
      let af = this.af;
      let folder = this.folder;
      let path = `/${this.folder}/${selectedFile.name}`;
      var iRef = storageRef.child(path);
      iRef.put(selectedFile).then((snapshot) => {
        console.log('Uploaded a blob or file! Now storing the reference at', `/${this.folder}/images/`);
        af.list(`/${folder}/images/`).push({ path: path, filename: selectedFile.name })
      });
    }

    // This part does not work
    iRef.getDownloadURL().then((url) => {this.image = url});
    console.log('IREF IS ' + iRef)
    console.log('IMAGEURL IS ' + this.image)

  }
Run Code Online (Sandbox Code Playgroud)

控制台日志是这些:

    IREF IS gs://my-app-159520.appspot.com/images/Screen Shot 2017-08-14 at 12.19.01.png
    view-order.component.ts:134 IMAGEURL IS undefined
Uploaded a blob or file! Now storing the reference at /images/images/
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用iRef引用来获取下载URL,但我一直在收到错误.我试图抓住网址,以便我可以将它分配给this.image变量,然后使用另一个函数将其存储在我的数据库中.

Fac*_*alm 11

API已更改。使用以下内容获取downloadURL

  snapshot.ref.getDownloadURL().then(function(downloadURL) {
    console.log("File available at", downloadURL);
  });
Run Code Online (Sandbox Code Playgroud)


Pee*_*Jee 8

从网络版本 v9(模块化)开始,您可以通过以下方式实现此目的:

import { getStorage, ref, uploadBytes, getDownloadURL } from "firebase/storage"; 

uploadImage = () => {
  const storage = getStorage()
  const reference = ref(storage, 'file_name.jpg')
  const file = e.target.files[0]

  uploadBytes(reference, file)
    .then(snapshot => {
      return getDownloadURL(snapshot.ref)
    })
    .then(downloadURL => {
      console.log('Download URL', downloadURL)
    })
}
Run Code Online (Sandbox Code Playgroud)


Bri*_*vie 5

我想我已经解决了这个问题,而且似乎可行,我意识到我必须从快照中获取downloadURL并将其分配给this.image,如下所示:

upload() {
    let storageRef = firebase.storage().ref();
    let success = false;

    for (let selectedFile of [(<HTMLInputElement>document.getElementById('file')).files[0]]) {
      let router = this.router;
      let af = this.af;
      let folder = this.folder;
      let path = `/${this.folder}/${selectedFile.name}`;
      var iRef = storageRef.child(path);
      iRef.put(selectedFile).then((snapshot) => {

        // added this part which as grabbed the download url from the pushed snapshot
        this.image = snapshot.downloadURL;

        console.log('Uploaded a blob or file! Now storing the reference at', `/${this.folder}/images/`);
        af.list(`/${folder}/images/`).push({ path: path, filename: selectedFile.name })

        console.log('DOWNLOAD URL IS ' + this.image)
      });
    }

  }
Run Code Online (Sandbox Code Playgroud)

然后,我运行我的其他功能以将URL添加到数据库中,并按预期进行了设置!

因此,我将图像上传到数据库,然后使用put函数中的快照,然后将变量image:any分配给快照downloadURL,如下所示:

this.image = snapshot.downloadURL;
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助其他人!


小智 5

.put()函数返回一个任务,可用于跟踪上传状态。

例如,您可以像这样监听进度、错误或完成:

onUploadImage () {
  const self = this
  const file = self.selectedFile
  if (!file) {
    return
  }
  self.isUploading = true
  const storageRef = firebase.storage().ref('/images/' + file.name)
  const task = storageRef.put(file)
  task.on('state_changed',
    function progress (snapshot) {
      self.status = 'UPLOADING...'
      self.percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
    },
    function error () {
      self.status = 'FAILED TRY AGAIN!'
      self.isUploading = false
    },

    function complete (event) {
      self.status = 'UPLOAD COMPLETED'
      self.isUploading = false
      storageRef.getDownloadURL().then((url) => { console.log(url) })
    }
  )
}
Run Code Online (Sandbox Code Playgroud)