upload image ionic 3

不羁岁月 提交于 2019-12-06 05:20:39

i have install "cordova-plugin-camera" : "^4.0.2" and "cordova-plugin-file": "^6.0.1"

fuction call here -> this.selectImage(this.camera.PictureSourceType.CAMERA);

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

options = {
  quality: 75,
  destinationType: this.camera.DestinationType.DATA_URL,
  sourceType: selection,
  allowEdit: true,
  encodingType: this.camera.EncodingType.JPEG,     
  saveToPhotoAlbum: false
};

this.camera.getPicture(options).then((imgUrl) => {

  if (options.destinationType == this.camera.DestinationType.FILE_URI) {
    console.log(imgUrl,'if');
    var sourceDirectory = imgUrl.substring(0, imgUrl.lastIndexOf('/') + 1);
    var sourceFileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length);
    sourceFileName = sourceFileName.split('?').shift();
    this.File.copyFile(sourceDirectory, sourceFileName, cordova.file.externalApplicationStorageDirectory, sourceFileName).then((result: any) => {
      this.imageNewPath = result.nativeURL;

      // do api call here

    }, (err) => {
      console.log(JSON.stringify(err));
    })
  }
  else {
    console.log(imgUrl,'else');
    this.imageNewPath = "data:image/jpeg;base64," + imgUrl;
    //do here 
  }
}, (err) => {
  console.log("Error in choosing image :" + JSON.stringify(err));
});

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