cstefanache / angular2-img-cropper

Angular 2 Image Cropper
MIT License
364 stars 135 forks source link

How can i get the cropped file? #272

Open AmirGilboa opened 6 years ago

AmirGilboa commented 6 years ago

in order to use ng2-file-upload to upload the cropped image to server, i need to get hold of the cropped image, and convert it to javascript File object. Any tip on how to do that?

webcoderr commented 6 years ago

You can take the cropped image from this.data.image. This will give you the base 64, then you can convert it to blob and then to a file.

Nervniyak commented 4 years ago

I used this code from SO to convert

private convertImageUriToFile(uri: string) {
    const arr = uri.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], this.fileName, { type: mime });
  }

Which I call with .image property this.convertImageUriToFile(this.imageData.image