简体   繁体   中英

Ionic/Firebase image-picker/Crop and upload issue

i was trying to implement the functionallity to pick an image and crop it. After that i would like to upload it to firebase. For some reason it doesnt work at all and the "readAsArrayBuffer" is not excecuting .

I am very new to this, so i woiuld appreciate some help :)

Thanks

This is what i tried:

"newPath" looks like this for example:

  "file:///storage/emulated/0/Android/data/io.ionic.starter/cache/
  1565766305015-cropped.jpg?1565766307602"

And "nameFile" look like this:

  "1565766305015-cropped.jpg".

-

    constructor(private imagePicker: ImagePicker, private crop: Crop,
        private file: File) {
          let storageDb = firebase.storage();
          this.storageRef = storageDb.ref();
      }


    pickImage() {
      this.imagePicker.getPictures(this.imagePickerOptions).then((results) 
    => {
        // tslint:disable-next-line: prefer-for-of
        for (let i = 0; i < results.length; i++) {
          this.cropImage(results[i]);
        }
      }, (err) => {
        alert(err);
      });
    }  


    cropImage(imgPath) {
        this.crop.crop(imgPath, { quality: 50 })
          .then(
            newPath => {
              try {
                let n = newPath.lastIndexOf("/");
                let x = newPath.lastIndexOf("g");
                let nameFile = newPath.substring(n + 1, x + 1);
                this.file.readAsArrayBuffer(newPath, nameFile).then((res) => {
                  let blob = new Blob([res], { type: "image/jpeg" });
                  var uploadTask = this.storageRef.child('images/' + this.event.id).put(blob);
                  uploadTask.on('state_changed', (snapshot) => {
                    let url = uploadTask.snapshot.downloadURL;
                    this.croppedImagepath = url;
                  }, (error) => {
                    alert("error: " + error);
                  }, () => { 
                    alert("uploaded");
                    let url = uploadTask.snapshot.downloadURL;
                    this.croppedImagepath = url;
                  })
                })
              }
              catch (z) {
                alert('error beim erstellen des blobs' + z);
              }
            },
            error => {
              alert('Error cropping image' + error);
            }
          );
      }

Cusious, where did you get the snippet from? There was actually another question that has come up and they are using the File class differently :

  readFile(file: any) {
    const reader = new FileReader();
    reader.onloadend = () => {
      const formData = new FormData();
      const imgBlob = new Blob([reader.result], {
        type: file.type
      });
      formData.append('file', imgBlob, file.name);
      this.uploadImageData(formData);
    };
    reader.readAsArrayBuffer(file);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM