«Хранение Firebase: недопустимый аргумент в` put` с индексом 0: ожидаемый BLOB-файл или файл. " - PullRequest
0 голосов
/ 24 декабря 2018

Я использую ionic 4, чтобы выбрать изображение, обрезать его, а затем загрузить в хранилище Firebase.Но у меня возникла проблема с загрузкой, потому что он говорит, что файл, который я загружаю, не относится ни к типу blob, ни к файлу.

В моем случае у меня есть локальный путь к изображению, которое я хочу загрузить.

Я преобразовал его в тип File: https://developer.mozilla.org/en-US/docs/Web/API/File, но Firebase по-прежнему не распознает его как File.

  press(){
    let options: ImagePickerOptions = {  
        //here Quality of images, defaults to 100  
        quality: 100,  
        //here Width of an Image  
        width: 640,  
        //here Height of an Image  
        height: 640,  
        /** Output type, defaults to 0 (FILE_URI). 

        * FILE_URI :0 (number) it returns a actual path for an image 

        */  
        outputType: 1, //0 is for image URI, 1 is for image base 64

        maximumImagesCount: 1//numbers  
        //while setting a number 15 we can load 15 images in one selection.  
    }; 

    this.imagePicker.getPictures({quality : 100, width: 640, height: 640, outputType: 0}).then((results) => {
      for (var i = 0; i < results.length; i++) {
        console.log('Image URI: ' + results[i]);

        this.crop.crop(results[i], {quality: 100, targetHeight: 640, targetWidth: 640})
        .then(
          newImage => {
            console.log('new image path is: ' + newImage),
            this.photoCroppedLocalUri = newImage;
            console.log(this.photoCroppedLocalUri);
            this.photoCroppedUri = this.webview.convertFileSrc(newImage);
            console.log('image path converted: ' + this.photoCroppedUri);

            this.assignFile().then(()=>{
              this.uploadFile(event);  
            })

          },
          error => console.error('Error cropping image', error)
        );
      }
    }, (err) => {
      console.log(err)
    });
  }

  assignFile(){ 
    return new Promise ((resolve, reject)=>{
      this.theFile = new File ([""], this.photoCroppedUri);
      resolve()
    })
  }

  uploadFile(event) {
    const file = this.theFile;
    const filePath = `photo/${this.authService.currentUserId}/photo01`;
    const fileRef = this.storage.ref(filePath);
    const task = this.storage.upload(filePath, file);

    // observe percentage changes
    this.uploadPercent = task.percentageChanges();
    // get notified when the download URL is available
    task.snapshotChanges().pipe(
        finalize(() => {
          this.downloadURL = fileRef.getDownloadURL() ; 
          console.log(this.downloadURL)
        })
     )
    .subscribe()
  }

Я надеюсь, что кто-то может помочь мне найти, что не так.Большое вам спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...