【问题标题】:How to upload photo taken on phone through devapp on firebase storage in Ionic 4如何通过devapp在Ionic 4的firebase存储上上传手机拍摄的照片
【发布时间】:2020-04-18 11:54:14
【问题描述】:

我正在尝试上传通过手机相机拍摄的照片(使用 Ionic 4 Native Camera Plugin 通过DevApp)并将其上传到 Firebase Storage。现在我可以拍照了,但是当我上传它时,控制台不会抛出任何错误,也不会做任何事情。最后,照片没有上传到firebase存储。

这是我的代码:

.html:

<ion-button (click)="takePicture()"></ion-button>

.ts:

  takePicture(){
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
     }

     this.camera.getPicture(options).then((imageData) => {
       var storage = firebase.storage().ref();
       var photoRef = storage.child('Manager Images'); 
       console.log(photoRef); <-- This reference is correct

       let base64Image = 'data:image/jpeg;base64,' + imageData;
       console.log(base64Image); <-- Returns "data:image/jpeg;base64,file:///storage/emulated/0/Android/data/io.ionic.devapp/cache/1577622281686.jpg"

       // Base64 formatted string
       var message = imageData;
       photoRef.putString(message , 'base64', { contentType: 'image/jpg' }).then((savedPicture) => {
       console.log(savedPicture.downloadURL);
});


   }, (err) => {
    // Handle error
   });
  }

我的 .putString 方法有问题吗?我参考了https://firebase.google.com/docs/storage/web/upload-files 的指导方针,但我仍然无法让它发挥作用。请帮忙!

【问题讨论】:

    标签: angular firebase ionic4 firebase-storage ionic-devapp


    【解决方案1】:

    将目标类型更改为this.camera.DestinationType.DATA_URL 以获取base64,然后将其放入firebase 存储中。

      takePicture(){
        const options: CameraOptions = {
          quality: 100,
          destinationType: this.camera.DestinationType.DATA_URL,
        ...
    
    

    附言。确定let base64Image = 'data:image/jpeg;base64,' + imageData; 是否必须,或者imageData 是否已经具有base64 格式的编码类型。

    【讨论】:

    • 感谢您的回答!有效!我保留了 base64Image 变量。
    【解决方案2】:

    更改此行,它应该可以工作。

    var message = base64Image;
    

    imageData 不是 'base64'

    或上传为文件,按原样使用文件。

    var file = imageData
    photoRef.put(file).then(snapshot => {
        console.log('Uploaded a blob or file!')
    });
    

    【讨论】:

    • 顺便说一句,当我为'message'变量执行console.log时,我得到了:“data:image/jpeg;base64,file:///storage/emulated/ 0/Android/data/io.ionic.devapp/cache/1577622281686.jpg" 我感觉是因为base64后面的链接,file:///...
    • 试试上面的。你在使用 Put 之前尝试过作为文件吗?
    • 试过了,还是不行。结果和以前一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多