首页 文章

当离子中图像上传为空时,无法读取未定义的属性“拆分”

提问于
浏览
0

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

**

NewpostPage.html:51 错误 ReferenceError:FileTransfer 未定义在 NewpostPage.webpackJsonp.162.NewpostPage.uploadPhoto(VM1295 main.js:601)位于 Object.eval [1](VM1527 NewpostPage.ngfactory.js:180)位于 handleEvent(VM1294 vendor.js:13963)处于 callWithDebugContext(VM1294 vendor.js:15472)Object.debugHandleEvent [2]处的 NewpostPage.webpackJsonp.162.NewpostPage.uploadPhoto(VM1295 main.js:601)处(VM1294 vendor.js:149642)(VM1294 vendor.js:149642) )at dispatchEvent(VM1294 vendor.js:10378)at VM1294 vendor.js:11003 at HTMLButtonElement。 (VM1294 vendor.js:39326)at 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 回答

  • 1

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

    在运行 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
    
    }
    

相关问题