Convert path to File or Blob Object Ionic


#1

Hi i am trying to crop and upload an image to firebase, i have the path of the file but i cant convert the image to File or Blob. somebody has a function for this ?
I am using the plugn cordova-plugin-crop.

this.cameraService.getMedia().then((x) =>{
      alert('then0.4 :' + x);
      try{
      let a = this.dataURItoBlob(x); //  this function is not working
          
      var uploadTask = this.storageRef.child('images-profile/'+this.email).put(a); 
});
// Thanks in advance for any help you are able to provide ;)

#2

thank to all :wink: xd . I lost a lot time in it, here is the solution. I use the plugin File for made a blob from the path.
Only clean the trys and caths

this.cameraService.getMedia().then((res) => {
      let path:string = res.toString();
      try{
        let n = path.lastIndexOf("/");
        let x = path.lastIndexOf("g");
        let nameFile = path.substring(n+1, x+1);
	      let directory = path.substring(0, n);
        alert("nombre archivo :" + nameFile + " *directory: " +directory.toString()+ " *allPath: " + res);
        File.readAsArrayBuffer(directory.toString(), nameFile).then((res) => {
          try{
            let blob = new Blob([res], {type: "image/jpeg"});
            var uploadTask = this.storageRef.child('images-profile/'+this.email).put(blob);
            uploadTask.on('state_changed', (snapshot) => {
                alert("change");
                url = uploadTask.snapshot.downloadURL;
                this.imageLocation = url;
            },(error) => {
              alert("NO subio imagen " + error);
              console.log("NO subio imagen " + error);
            }, () => {//success
              alert("subio imagen");
              url = uploadTask.snapshot.downloadURL;
              this.imageLocation = url;
            });
          }catch(z){
            alert('error al crear blob' + z);
          }
        }).catch(err => alert('error al leer el archivo ' + JSON.stringify(err)));
      }catch(z){
        alert(z);
      }
    });