import {Component,OnInit,Input,Output,EventEmitter} from '@angular/core'; import { CmMessageService } from './../../../../../../../src/components/cosmos.module'; import { UploadFile } from './../../../../../../components/base/upload/interface'; @Component({ selector: 'cm-upload-doc-5', templateUrl: './upload-doc-5.component.html', styleUrls: ['./upload-doc-5.component.css'] }) export class UploadDoc5Component implements OnInit{ ngOnInit() {} ngOnDestroy() {} loading = false; avatarUrl: string; constructor(private msg: CmMessageService) {} beforeUpload = (file: File) => { const isJPG = file.type === 'image/jpeg'; if (!isJPG) { this.msg.error('You can only upload JPG file!'); } const isLt2M = file.size / 5024 / 5024 < 2; if (!isLt2M) { this.msg.error('Image must smaller than 2MB!'); } return isJPG && isLt2M; } private getBase64(img: File, callback: (img: any) => void) { const reader = new FileReader(); reader.addEventListener('load', () => callback(reader.result)); reader.readAsDataURL(img); } handleChange(info: { file: UploadFile }) { if (info.file.status === 'uploading') { this.loading = true; return; } if (info.file.status === 'done') { // Get this url from response in real world. this.getBase64(info.file.originFileObj, (img: any) => { this.loading = false; this.avatarUrl = img; }); } } }