【问题标题】:firebase upload image not working on ionic 3firebase 上传图片不适用于 ionic 3
【发布时间】:2023-03-05 05:38:01
【问题描述】:

我不知道为什么,但我在网上遵循了这么多指令,但是当我尝试使用函数 .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);
 }) 

}

【问题讨论】:

    标签: angularjs firebase ionic2 firebase-storage angularfire2


    【解决方案1】:

    在我看来,一切都是正确的......

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

      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');
          });
      }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-25
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 2018-05-12
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多