uploadTask.snapshot.downloadURL没有提供文件的下载URL作为响应

时间:2019-02-26 11:22:59

标签: javascript firebase firebase-storage angular7

此刻我正在做一个弯角课程,将档案上传到Firebase时遇到问题,我可以上传文件,但没有得到下载URL作为响应:

import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList,
        AngularFireObject } from 'angularfire2/database';
import * as firebase from 'firebase';
import { Archivo } from '../uploads/file.modal';
@Injectable({
  providedIn: 'root'
})
export class LoadfileService {

  private basePath: string = '/uploads';
  uploads: AngularFireList<Archivo[]>;

  constructor( public angularFireDatabase: AngularFireDatabase) { }

  pushUpload(upload: Archivo) {
    const storageRef = firebase.storage().ref();
    const uploadTask = storageRef.child(`${this.basePath}/${upload.file.name}`).put(upload.file);

    uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot) =>{
        upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes * 100);
      },
      (error) => {
        console.log(error);
      },
      () => {
    // tslint:disable-next-line: deprecation
            upload.url = uploadTask.snapshot.downloadURL;
            upload.name = upload.file.name;
            this.saveFileData(upload);
          });
      }

      private saveFileData(upload: Archivo) {
        this.angularFireDatabase.list(`${this.basePath}/`).push(upload);
      }
 }

这是我在上传服务中使用的代码,它告诉我donwloadURL已弃用。

我正在寻找解决方案的atm,但找不到任何有用的东西。

1 个答案:

答案 0 :(得分:1)

谷歌搜索一段时间后问题解决:

      pushUpload(upload: Archivo) {
    const storageRef = firebase.storage().ref();
    const uploadTask = storageRef.child(`${this.basePath}/${upload.file.name}`).put(upload.file);

    storageRef.child(`${this.basePath}/${upload.file.name}`).getDownloadURL().then(ref => {
      this.urlDownload = ref;
    });

    uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot) =>{
        upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes * 100);
      },
      (error) => {
        console.log(error);
      },
      () => {
        upload.url = this.urlDownload;
        upload.name = upload.file.name;
        this.saveFileData(upload);
      });
  }