Not able to convert the base64 to image path in Ionic 4

好久不见. 提交于 2019-12-02 00:10:24

Please try this. You can use file transfer Native Plugin for this. It will solve your problem.

In the ts file:

async UpdateUserDetails(){ 
   if(this.imagepic && this.fileUploadName) {
    let  fileTransfer = this.transfer.create();
    let options: FileUploadOptions = {
      fileKey: 'profile_pic',
      fileName: this.fileUploadName,
      headers: {}     
  }  
  options.params = {
    first_name: this.userdetailsedit.value.first_name,
    last_name: this.userdetailsedit.value.last_name,
    mobile: this.userdetailsedit.value.mobile,
    old_password:  this.userdetailsedit.value.old_password,
    password: this.userdetailsedit.value.password,    
  };
  this.storage.get('USER').then(userde => {
    if (userde) {
    this.userdetails = userde;
   fileTransfer.upload(this.imagepic, this.apiUrl+'userUpdateProfile/'+this.userdetails.id, options)
   .then((data) => {
     if(data && data.responseCode==200){
      let response=JSON.parse(data.response);
      if(response.status === "success"){
      this.storage.set('ID', response.data.id);
      this.storage.set('USER', response.data);
      this.modalController.dismiss();
      } else{
        loading2.dismiss();
      }
     }else{
       //show error msg  
     }
   }, (err) => {     
    console.log('upload err ::',err);
   });
  }
 });
}
}
async imageuserchoose(sourceType){
    const options: CameraOptions = {
      quality: 76,
      sourceType: sourceType,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: true,
      correctOrientation: true,
      // allowEdit: true,
    }

    this.camera.getPicture(options).then((imageData) => {    
      let filename,path;
      this.imagepic = imageData;
        if (sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
             path = imageData.substring(0, imageData.lastIndexOf('/') + 1);
             filename = imageData.substring(imageData.lastIndexOf('/') + 1);
            let index = filename.indexOf('?');     
            if (index > -1) {
              filename = filename.substring(0,index);
            }      
        }
        if (sourceType === this.camera.PictureSourceType.CAMERA) {
             filename = imageData.substring(imageData.lastIndexOf('/') + 1);
             path = imageData.substring(0, imageData.lastIndexOf('/') + 1);
            console.log(path,'FileName::', filename);            
        }
        this.fileUploadName=filename;
        this.file.readAsDataURL(path, filename).then(data => {          
          this.userdetailsedit.patchValue({
            profile_pic: data,
          }); 
      });
    }, (err) => {
     // Handle error
    });
  }

This will solve your problem.

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