ng-ytl-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
82 lines (73 loc) • 2.24 kB
text/typescript
// tslint:disable
import { Component } from '@angular/core';
import { NzMessageService } from './../../components/message/nz-message.service';
import { UploadFile } from './../../components/upload/interface';
export class NzDemoUploadAvatarComponent {
loading = false;
avatarUrl: string;
constructor(private msg: NzMessageService) {}
beforeUpload = (file: File) => {
const isJPG = file.type === 'image/jpeg';
if (!isJPG) {
this.msg.error('You can only upload JPG file!');
}
const isLt2M = file.size / 1024 / 1024 < 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;
});
}
}
}