【问题标题】:How to upload pictures to firebase in Ionic 4?如何在 Ionic 4 中将图片上传到 Firebase?
【发布时间】:2019-03-12 20:20:17
【问题描述】:

我无法将图片上传到 Firebase。任何帮助将不胜感激。

有人熟悉如何在 Ionic 4.0 中将图片上传到 firebase 吗?下面是用于在 Ionic 2 中工作的代码,但是现在当我单击按钮将图像上传到 firebase 时,响应(单击)大约需要 30 秒,然后它永远不会将图像上传到 firebase。

我尝试使用本教程中的示例,但无法摆脱与 ImagePicker 相关的错误。 https://ionicthemes.com/tutorials/about/ionic-firebase-image-upload 任何帮助将不胜感激。

      <ion-card-content>
            <div>
                <img src="assets/img/add-an-image.png" (click)="selectPhoto()"/>     
            </div>
        </ion-card-content>

constructor(
    private afAuth: AngularFireAuth,
    private camera: Camera) {

        this.afAuth.authState.subscribe(user => {
        this.userId = user.uid;
        this.myPhotosRef = firebase.storage().ref(`/Photos/${ this.userId }/`);
      });
    }

  selectPhoto(): void {
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
    };

    this.camera.getPicture(options).then((imageData) => {
      console.log(options, 'get pic');
      this.myPhoto = imageData;
      this.uploadPhoto(this.myPhoto);
    }, error => {
      console.log('ERROR -> ' + JSON.stringify(error));
    });
  }

  private uploadPhoto(photoName: string): void {
    this.myPhotosRef.child(photoName)
      .putString(this.myPhoto, 'base64', { contentType: 'image/png' })
      .then((savedPicture) => {
        this.myPhotoURL = savedPicture.downloadURL;
      });
  }

【问题讨论】:

  • 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。
  • 你说,"I tried using the example in this tutorial"。你指的是什么教程?

标签: angular firebase ionic4


【解决方案1】:

【讨论】:

    【解决方案2】:

    确实有很多方法可以做到这一点,您可以使用响应和错误来跟踪正在发生的事情。启用 Web 控制台日志。

    以下是如何执行此操作和跟踪错误的示例:

    uploadFile(file){
    const fileName = this.generateRandomName() + '.jpg';
    const fileRef = firebase.storage().ref().child('tptimagefolder/'+ fileName);
    const uploadTask = fileRef.put(file);
    
    return new Promise((resolve, reject) => {
        uploadTask.on('state_changed', snapshot => {
    },  error => {
          reject(error);
    
    }, () => {
      uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
        console.log('teste resolve url'+downloadURL);
    
        resolve ( {fileName, downloadURL});
      });
    
    });
    
    }); }
    

    this.generateRandomName() 是一个创建名称的函数,如果需要,可以不使用它。

    【讨论】:

      【解决方案3】:

      我能够使用以下教程成功地将图像上传到 Firebase:

      https://ionicthemes.com/tutorials/about/building-a-ionic-firebase-app-step-by-step

      【讨论】:

        【解决方案4】:

        我按照 Ionic Themes 的本教程将图像上传到 Firebase 存储:https://ionicthemes.com/tutorials/about/ionic-firebase-image-upload

        它说它适用于 Ionic 3,但它也适用于 Ionic 4。

        根据教程,Storage 与 firestore 和 fire storage 模块是分开的,所以在你的 app 模块中,你必须像这样导入 firebase:

        import * as firebase from "firebase";
        firebase.initializeApp(environment.firebaseConfig)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-25
          • 1970-01-01
          • 1970-01-01
          • 2020-06-24
          • 2019-11-03
          • 2019-10-30
          • 2018-04-09
          • 2016-09-27
          相关资源
          最近更新 更多