0

我正在尝试上传通过手机相机拍摄的照片Camera Plugin(通过DevApp使用 Ionic 4 Native )并将其上传到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的指南,但我仍然无法让它工作。请帮忙!

4

2 回答 2

1

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

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

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

于 2019-12-29T15:08:54.620 回答
0

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

var message = base64Image;

imageData 不是'base64'

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

var file = imageData
photoRef.put(file).then(snapshot => {
    console.log('Uploaded a blob or file!')
});
于 2019-12-29T11:48:12.033 回答