尝试获取downloadUrl但上传文件时出现GET错误

时间:2019-12-30 07:58:19

标签: angular firebase google-cloud-firestore angular8

我的ts文件

import { TimetableService } from './../../shared/timetable.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { NgForm } from '@angular/forms';
import { AngularFireStorage } from '@angular/fire/storage';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'app-timetable',
  templateUrl: './timetable.component.html',
  styleUrls: ['./timetable.component.css']
})
export class TimetableComponent implements OnInit {

  selectedFile =null;
  uploadPercent: Observable<number>;
  downloadURL: Observable<string>;

  constructor(private storage: AngularFireStorage, private firebase: AngularFirestore, private service: TimetableService) { }

  ngOnInit() {
    this.resetForm();

  }

  resetForm(form?: NgForm){
    if(form!=null)
    form.resetForm();
    this.service.timetable ={
      id: "",
      title:"",
      imgurl:""
    };
  }

  async onSubmit(form: NgForm){
    // console.log(form.value.id);
    // console.log(form.value.title);
    console.log(form.value.imgurl);
    let dataa = Object.assign({},form.value);// id, imgurl, title
    delete dataa.id;

    const filePath = 'timetable'+dataa.title;
    const fileRef = this.storage.ref(filePath);
    const task = this.storage.upload(filePath, this.selectedFile);

    // observe percentage changes
    this.uploadPercent = task.percentageChanges();
    // get notified when the download URL is available
    task.snapshotChanges().pipe(
        finalize(() => this.downloadURL = fileRef.getDownloadURL()
         )
     )
    .subscribe();

    //dataa.imgurl = "test";
    console.log(fileRef.getDownloadURL());
    this.firebase.collection('timetabledata').add(dataa);

  }

  uploadImage(event){
    this.selectedFile = event.target.files[0];
  }

}

在这里,我尝试将图像上传到Firebase存储并获取downloadUrl。即使我的图片已上传,我也无法获得下载网址。

在这里,我已经在控制台中输出了downloadUrl。它发出以下错误。

GET https://firebasestorage.googleapis.com/v0/b/gdis-9f4df.appspot.com/o/timetablejjj 404
ERROR FirebaseStorageError {code_: "storage/object-not-found", message_: "Firebase Storage: Object 'timetablejjj' does not exist.", serverResponse_: "{↵  "error": {↵    "code": 404,↵    "message": "No…not get object",↵    "status": "GET_OBJECT"↵  }↵}", name_: "FirebaseError"}

当我导航到错误消息中给出的URL(https://firebasestorage.googleapis.com/v0/b/gdis-9f4df.appspot.com/o/timetablejjj)时,它显示了这样的对象,

enter image description here 并且我的存储中以“ timetablejjj”的名称上传了图像。

这是什么问题?我该如何解决?

0 个答案:

没有答案