upload-image-with-preview.component.ts 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import {Component, EventEmitter, Input, OnChanges, OnDestroy, Output} from '@angular/core';
import { NzMessageService, UploadXHRArgs, UploadFile } from 'ng-zorro-antd';


@Component({
  selector: 'app-upload-image-with-preview',
  templateUrl: './upload-image-with-preview.component.html',
  styleUrls: ['./upload-image-with-preview.component.scss']
})
export class UploadImageWithPreviewComponent implements OnDestroy, OnChanges {
  uploading = false;
  progress = 0;
  @Input()
  picUrl;
  @Input()
  canDelete = false;
  @Output()
  imageUploaded = new EventEmitter();
  @Output()
  imageUploadFailure = new EventEmitter();
  @Output()
  delete = new EventEmitter();
  @Input()
  picItem = null;
  @Input()
  iconSize = 2;
  @Input()
  TIP = 'Click here to upload image';
  @Input()
  disableUpload = false;

32 33
  uploadUrl;
  uploadData;
34 35

  constructor(private nzMessageService: NzMessageService) {
36 37 38 39 40 41 42 43 44

    this.uploadUrl = (<any> window).courseware.uploadUrl();
    this.uploadData = (<any> window).courseware.uploadData();

    window['air'].getUploadCallback = (url, data) => {
      this.uploadUrl = url;
      this.uploadData = data;
    };

limingzhe's avatar
limingzhe committed
45
    this.setUploadUrl();
46
  }
limingzhe's avatar
limingzhe committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


  setUploadUrl() {

    if (!this.uploadUrl) {
      console.log('this.uploadUrl -=- 1 : ', this.uploadUrl)
      this.uploadUrl = (<any> window).courseware.uploadUrl();
      this.uploadData = (<any> window).courseware.uploadData();
      console.log('this.uploadUrl -=- 2 : ', this.uploadUrl)

      setTimeout(() => {
        this.setUploadUrl();
      }, 500);
    }

    console.log('this.uploadUrl -=- 3 : ', this.uploadUrl)

  }


67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  ngOnChanges() {
    if (!this.picItem) {
      return;
    }
  }





  handleChange(info: { type: string, file: UploadFile, event: any }): void {

    console.log('info:' , info);

    switch (info.type) {
      case 'start':
        // this.isUploading = true;
        // this.progress = 0;

        this.picUrl = null;
        // this.beforeUpload(item.file as any);
        if (!this.checkSelectFile(info.file)) {
          return;
        }
        this.uploading = true;
        this.progress = 0;

        break;

      case 'success':
        // this.isUploading = false;
        // this.uploadSuccess(info.file.response);
        // this.audioUploaded.emit(info.file.response);

        this.uploadSuccess(info.file);

        break;
      case 'progress':
        this.progress = parseInt(info.event.percent, 10);
        this.doProgress(this.progress);
        break;
    }
  }

  onDelete() {
    this.delete.emit();
  }
  checkSelectFile(file) {
    const isImg = ['image/jpeg', 'image/png', 'image/jpeg', 'image/gif', 'image/bmp'].includes(file.type);
    if (!isImg) {
      this.nzMessageService.error('You can only upload Image file (jpg|gif|png|bmp)');
      return false;
    }
    const isGif = !['image/jpeg', 'image/png', 'image/jpeg', 'image/bmp'].includes(file.type);
    const delta = isGif ? 20 : 5;
    const isOverSize = file.size / 1024 / 1024 < delta;
    if (!isOverSize) {
      this.nzMessageService.error(`${isGif ? 'Gif' : 'Image'} must smaller than ${delta}MB!`);
      return false;
    }
    return true;
  }

  uploadSuccess = (file) => {
    this.nzMessageService.info('Upload Success');
    this.uploading = false;
    this.picUrl = file.response.url ;
    // this.uploadFinished(url);
    // if (!inOSS) {
    const img = new Image();
    img.addEventListener('load', () => {

      const height = img.naturalHeight;
      const width = img.naturalWidth;
      file['height'] = height;
      file['width'] = width;
      img.remove();
      this.imageUploaded.emit(file.response);
      // this.resService.updateImage(id, {width, height}).then( () => {
      //   this.imageUploaded.emit({res_id: id, id, name, hash, url});
      // });

    });
    img.src = file.response.url;
    // } else {
    //   this.imageUploaded.emit({res_id: id, id, name, hash, url});
    // }
  }

  uploadFailure = (err, file) => {
    this.uploading = false;
    if (err.name && err.name === 'cancel') {
      return;
    }
    console.log(err);
    this.nzMessageService.error('Upload Error ' + err.message);
    this.imageUploadFailure.emit(file);
  }

  doProgress = function(p) {
    if (p > 1) {
      p = 1;
    }
    if (p < 0) {
      p = 0;
    }
    // console.log(Math.floor(p * 100));
    this.progress =  Math.floor(p * 100);
  }
  ngOnDestroy() {
  }

}