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

前端 未结 1 412
甜味超标
甜味超标 2021-01-23 01:07

I am working on my Ionic 4 app and I have used the camera plugin for image uploading and I have converted the image to the base64 for showing the image but the problem is that I

1条回答
  •  执笔经年
    2021-01-23 01:40

    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.

    0 讨论(0)
提交回复
热议问题