无法读取 属性 'split' of undefined 当 ionic 中的图片上传为空时

Cannot read property 'split' of undefined when image upload is empty in ionic

我尝试在浏览器中测试我的离子图像上传应用程序,但由于 cordova_not_available 显示在屏幕上,我无法上传图像,每次我点击上传按钮时都会弹出此错误 无法读取未定义的 属性'split'

**

NewpostPage.html:51 ERROR ReferenceError: FileTransfer is not defined at new Transfer (VM1294 vendor.js:149642) at NewpostPage.webpackJsonp.162.NewpostPage.uploadPhoto (VM1295 main.js:601) 在 Object.eval [as handleEvent] (VM1527 NewpostPage.ngfactory.js:180) 在 handleEvent (VM1294 vendor.js:13963) 在 callWithDebugContext (VM1294 vendor.js:15472) 在 Object.debugHandleEvent [as handleEvent] (VM1294 vendor.js:15059) 在 dispatchEvent (VM1294 vendor.js:10378) 在 VM1294 vendor.js:11003 在 HTMLButtonElement。 (VM1294 vendor.js:39326) 在 t.invokeTask (VM1427 polyfills.js:3)

**

在我的 upload.ts 我有这个

 uploadPhoto() {
    let loader = this.loadingCtrl.create({
      content: "Please wait..."
    });
    loader.present();

    //let filename = this.imagePath.split('/').pop();
    console.log('this.imagePath: ', this.imagePath)
    let filename = this.imagePath;
    let options = {
      fileKey: "file",
      fileName: filename,
      chunkedMode: false,
      mimeType: "image/jpg",
      params: {'location': this.location, 'title': this.postTitle, 'description': this.desc }
    };


    const fileTransfer = new Transfer();

    fileTransfer.upload(this.imageNewPath, AppSettings.API_UPLOAD_ENDPOINT,
      options).then((entry) => {
        this.imagePath = '';
        this.imageChosen = 0;
        loader.dismiss();
        this.navCtrl.setRoot(IncidentsPage);
      }, (err) => {
        alert(JSON.stringify(err));
      });
  }

  chooseImage() {

    let actionSheet = this.actionSheet.create({
      title: 'Choose Picture Source',
      buttons: [
        {
          text: 'Gallery',
          icon: 'albums',
          handler: () => {
            this.actionHandler(1);
          }
        },
        {
          text: 'Camera',
          icon: 'camera',
          handler: () => {
            this.actionHandler(2);
          }
        },
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });

    actionSheet.present();
  }


  //}

  actionHandler(selection: any) {
    var options: any;

    if (selection == 1) {
      options = {
        quality: 75,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 500,
        targetHeight: 500,
        saveToPhotoAlbum: false
      };
    } else {
      options = {
        quality: 75,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 500,
        targetHeight: 500,
        saveToPhotoAlbum: false
      };
    }

    Camera.getPicture(options).then((imgUrl) => {

      var sourceDirectory = imgUrl.substring(0, imgUrl.lastIndexOf('/') + 1);
      var sourceFileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length);
      sourceFileName = sourceFileName.split('?').shift();
      File.copyFile(sourceDirectory, sourceFileName, cordova.file.externalApplicationStorageDirectory, sourceFileName).then((result: any) => {
        this.imagePath = imgUrl;
        this.imageChosen = 1;
        this.imageNewPath = result.nativeURL;

      }, (err) => {
        alert(JSON.stringify(err));
      })

    }, (err) => {
      alert(JSON.stringify(err))
    });

  }

请大家帮忙

我想我现在明白你在问什么了。

您需要在 运行 uploadPhoto() 中的代码之前检查 undefined 和 ""。

uploadPhoto() {

if(this.imagePath !== undefined || this.imagePath !== '') {
let loader = this.loadingCtrl.create({
      content: "Please wait..."
    });
    loader.present();

    //let filename = this.imagePath.split('/').pop();
    console.log('this.imagePath: ', this.imagePath)
    let filename = this.imagePath;
    let options = {
      fileKey: "file",
      fileName: filename,
      chunkedMode: false,
      mimeType: "image/jpg",
      params: {'location': this.location, 'title': this.postTitle, 'description': this.desc }
    };


    const fileTransfer = new Transfer();

    fileTransfer.upload(this.imageNewPath, AppSettings.API_UPLOAD_ENDPOINT,
      options).then((entry) => {
        this.imagePath = '';
        this.imageChosen = 0;
        loader.dismiss();
        this.navCtrl.setRoot(IncidentsPage);
      }, (err) => {
        alert(JSON.stringify(err));
      });
  }
} else {
    //do something else

}