Ionic 4 file upload error with FileReader / ios

南笙酒味 提交于 2019-12-13 03:21:56

问题


I am trying to upload an image using ionic 4 and ios.

The error I keep getting is:

TypeError: undefined is not a constructor (evaluating 'new _ionic_native_file_ngx__WEBPACK_IMPORTED_MODULE_2__["FileReader"]()')

Here is the relevant code:

  startUpload(imgEntry) {
    this.file.resolveLocalFilesystemUrl(imgEntry.filePath)
    .then(entry => {
      (<FileEntry> entry).file(file => this.readFile(file));
    }).catch(err => {
      this.presentToast('Error while reading 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);
  }

  async uploadImageData(formData: FormData) {
    const loading = await this.loadingController.create({
      message: 'Uploading image...'
    });
    await loading.present();

    this.http.post('http://localhost/upload.php', formData)
    .pipe(
      finalize(() => {
        loading.dismiss();
      })
    )
    .subscribe(res => {
      if (res['success']) {
        this.presentToast('File upload complete.');
      } else {
        this.presentToast('File upload failed.');
      }
    });
  }

I am copying the app to my iphone 6 via xcode and then running the application, which runs fine but when I try to upload the image I get the error above.
How can I fix this error?

来源:https://stackoverflow.com/questions/57485613/ionic-4-file-upload-error-with-filereader-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!