代码之家  ›  专栏  ›  技术社区  ›  Tonye Boro

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

  •  0
  • Tonye Boro  · 技术社区  · 8 年前

    我尝试在浏览器中测试我的ionic图像上载应用程序,但由于屏幕上显示了cordova不可用,因此无法上载图像,每次单击上载按钮时都会弹出此错误 无法读取未定义的属性“split”

    **

    NewpostPage.html:51错误引用错误:在NewpostPage.webpackJsonp.162.NewpostPage.uploadPhoto(VM1295 main.js:601)的new Transfer(VM1294 vendor.js:149642)处,在callWithDebugContext(VM1294 vendor.js:13963)的handleEvent(VM1527 NewpostPage.ngfactory.js:180)处的Object.eval[作为handleEvent](vm1297vendor.js:15472)位于Object.debugHandleEvent[作为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)

    **

    上传.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 回复  |  直到 8 年前
        1
  •  1
  •   Shannon    8 年前

    我想我明白你现在的要求了。

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

    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
    
    }