angular2-img-cropper icon indicating copy to clipboard operation
angular2-img-cropper copied to clipboard

How to upload with file uploaders

Open kolkov opened this issue 7 years ago • 2 comments

Hi! How to upload resized image with file uploader like this package: https://github.com/jkuri/ngx-uploader

Or is there a better solution?

kolkov avatar May 23 '17 14:05 kolkov

This is what I did:

  • Tell the cropper component not to render the this.cropperSettings.noFileInput = true;

  • Use standard HTML5 input element <label class="btn btn-primary" > <input type="file" #fileInput accept="image/*" (change)="uploadPhoto($event)" style="display: none;"> Upload Photo </label>

  • and then in the 'uploadPhoto'

public uploadPhoto($event): void {
    const image = new Image();
    const file: File = $event.target.files[0];
    const myReader: FileReader = new FileReader();
    const scope = this;

    if (!file) {
      return;
    }

    myReader.onloadend = function (loadEvent: any) {
      image.src = loadEvent.target.result;
      scope.cropper.setImage(image)
    };

    myReader.readAsDataURL(file);
  }

  • Where 'cropper' is decorated with the '@ViewChild' @ViewChild('cropper') public cropper: ImageCropperComponent;

vajnorcan avatar Jun 01 '17 23:06 vajnorcan

it's shows an ERROR TypeError: Cannot read property 'setImage' of undefined, at scope.cropper.setImage(image)

ZikZakJainy avatar Sep 01 '17 06:09 ZikZakJainy