簡體   English   中英

Ionic / Firebase圖像選擇器/裁剪和上傳問題

[英]Ionic/Firebase image-picker/Crop and upload issue

我試圖實現功能以選擇圖像並對其進行裁剪。 之后,我想將其上傳到Firebase。 由於某種原因,它根本不起作用,並且“ readAsArrayBuffer”沒有執行。

我對此很陌生,所以我將不勝感激:)

謝謝

這是我嘗試的:

例如,“ newPath”看起來像這樣:

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

而且“ nameFile”看起來像這樣:

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

求求您,您從何處獲得摘錄? 實際上還有另一個問題, 他們使用File類的方式有所不同

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM