Firebase 上传图片在 Ionic 3 上不起作用



我不知道为什么,但我在网上遵循了很多说明,但当我尝试为函数 .putString 使用第二个参数时,似乎我无法使用它它总是返回此错误:

[object Object]
at viewWrappedDebugError (core.js:9503)
at callWithDebugContext (core.js:14749)
at Object.debugHandleEvent [as handleEvent] (core.js:14326)
at dispatchEvent (core.js:9703)
at core.js:10317
at HTMLButtonElement.<anonymous> (platform-browser.js:2614)
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (core.js:4617)
at t.invokeTask (polyfills.js:3)
at r.runTask (polyfills.js:3)

这对我没有帮助,所以任何人都可以帮助我这是我的代码:

captureDataUrl: string;
capture() {
 //setup camera options
 const cameraOptions: CameraOptions = {
   quality: 50,
   destinationType: this.camera.DestinationType.DATA_URL,
   encodingType: this.camera.EncodingType.JPEG,
   mediaType: this.camera.MediaType.PICTURE,
 };
 this.camera.getPicture(cameraOptions).then((imageData) => {
   // imageData is either a base64 encoded string or a file URI
   // If it's base64:
   this.captureDataUrl = 'data:image/jpeg;base64,' + imageData;
   alert(this.captureDataUrl);
 }, (err) => {
    alert(err);
 });
}
upload() : AngularFireUploadTask {
const filename = 'filename';
this.afStorage.ref(`users/${filename}.jpg`)
 .putString(this.captureDataUrl,'data_url')
 .then((snapshot)=>{
    alert(snapshot);
 })
 .catch((err)=>{
    alert(err);
 }) 
}

从我的角度来看,一切都是正确的...

在这里你可以找到我的方法:

  public refPhoto: firebase.storage.Reference = firebase.storage().ref('/Photos');
  public myPhoto: any;

  //Camera and upload
  takePhoto(uid: string) {
    Camera.getPicture({
      quality: 100,
      destinationType: Camera.DestinationType.DATA_URL,
      sourceType: Camera.PictureSourceType.CAMERA,
      encodingType: Camera.EncodingType.PNG,
      saveToPhotoAlbum: true
    }).then(imageData => {
      this.myPhoto = imageData;
      this.uploadPhoto(uid);
    }).catch((err) => {
      console.log(err);
      console.log('Cant take photo');
    });
  }
  selectPhoto(uid: string): void {
    Camera.getPicture({
      sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: Camera.DestinationType.DATA_URL,
      quality: 100,
      encodingType: Camera.EncodingType.PNG,
    }).then(imageData => {
      this.myPhoto = imageData;
      this.uploadPhoto(uid);
    }).catch((err) => {
      console.log(err);
      console.log('Cant select photo');
    });
  }
  private uploadPhoto(uid: string): void {
    this.refPhoto.child(this.fire.auth.currentUser.uid).child('profileImage.png')
      .putString(this.myPhoto, 'base64', { contentType: 'image/png' })
      .then((savedPicture) => {
        //good practice is to store this reference ID to Database
      }).catch((err) => {
        console.log(err);
        console.log('Cant upload photo');
      });
  }

希望对您有所帮助!

相关内容

  • 没有找到相关文章

最新更新