当图像上载为空时无法读取未定义的属性“ split”

时间:2018-08-14 00:30:41

标签: ionic-framework ionic2

我尝试在浏览器中测试离子图像上载应用程序,但是由于由于cordova_not_available出现在屏幕上而导致无法上载图像,因此每次我单击上载按钮上的ed时都会弹出此错误无法读取属性“拆分”的未定义

**

NewpostPage.html:51错误ReferenceError:未在Object.eval的NewpostPage.webpackJsonp.162.NewpostPage.uploadPhoto(VM1295 main.js:601)的新传输(VM1294 vendor.js:149642)上定义FileTransfer。 handleEvent](VM1527 NewpostPage.ngfactory.js:180)在handleEvent(VM1294 vendor.js:13963)在callWithDebugContext(VM1294 vendor.js:15472)在Object.debugHandleEvent [在handleEvent](VM1294 vendor.js:15059)在(HTML129Element的VM1294 vendor.js:11003)(VM1294 vendor.js:10378)。 (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))
    });

  }

请帮忙。

1 个答案:

答案 0 :(得分:1)

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

在运行uploadPhoto()中的代码之前,您需要检查未定义和“”。

{{1}}