1- import { Directive , ElementRef , EventEmitter , Input , Output , OnInit , OnDestroy } from '@angular/core' ;
1+ import {
2+ Directive ,
3+ ElementRef ,
4+ EventEmitter ,
5+ Input ,
6+ Output ,
7+ OnInit ,
8+ OnDestroy ,
9+ } from '@angular/core' ;
210import { Converted } from './converted' ;
311
412@Directive ( {
5- selector : '[fileToBase64]'
13+ selector : '[fileToBase64]' ,
614} )
715export class FctrlxFileToBase64Directive implements OnInit , OnDestroy {
816
917 @Input ( ) files : any ;
1018 @Input ( ) type : string ;
11- @Input ( ) multiple : any ;
19+ @Input ( ) multiple : undefined | null | string | boolean ;
1220
1321 @Output ( ) filesChange : EventEmitter < any > = new EventEmitter ( ) ;
1422
1523 private readonly TYPE_FILE : string = 'file' ;
1624
17- private converted : Array < Converted > = [ ] ;
25+ private converted : Converted [ ] = [ ] ;
1826 private currentIndex : number = 0 ;
1927
2028 constructor ( private element : ElementRef ) { }
2129
2230 ngOnInit ( ) : void {
23- if ( this . type === this . TYPE_FILE ) {
31+ if ( this . type === this . TYPE_FILE && this . isSupported ) {
2432 this . element . nativeElement . addEventListener ( 'change' , this . filesChanged . bind ( this ) , false ) ;
25- }
26- }
33+ } else {
34+ let msg : string = 'fileToBase64 ' ;
2735
28- get isMultiple ( ) : boolean {
29- return ! ( typeof this . multiple === 'undefined' ) ;
36+ if ( ! this . isSupported ) {
37+ msg += 'is not supported by your browser.' ;
38+ } else {
39+ msg += 'working only with input type=file.' ;
40+ }
41+
42+ console . warn ( msg , this . element . nativeElement ) ;
43+ }
3044 }
3145
3246 filesChanged ( event : Event ) : void {
@@ -35,17 +49,20 @@ export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
3549 this . converted = [ ] ;
3650 this . currentIndex = 0 ;
3751
38- Object . keys ( files ) . forEach ( ( key : string ) => {
52+ Object . keys ( files ) . forEach ( ( key : string ) => {
3953 const reader = new FileReader ( ) ;
40- reader . onload = ( file ) => this . storeBase64 ( file ) ;
54+
55+ reader . onload = ( file ) => {
56+ this . storeBase64 ( file ) ;
57+ } ;
4158
4259 const { name, size, type, base64 } = files [ key ] ;
4360
4461 this . converted . push ( {
4562 name,
4663 size,
4764 type,
48- base64
65+ base64,
4966 } ) ;
5067
5168 reader . readAsDataURL ( files [ key ] ) ;
@@ -56,7 +73,16 @@ export class FctrlxFileToBase64Directive implements OnInit, OnDestroy {
5673
5774 storeBase64 ( file : { target } ) {
5875 this . converted [ this . currentIndex ] . base64 = file . target . result ;
59- this . currentIndex ++ ;
76+ this . currentIndex = this . currentIndex + 1 ;
77+ }
78+
79+ get isMultiple ( ) : boolean {
80+ return ! ( typeof this . multiple === 'undefined' ) ;
81+ }
82+
83+ get isSupported ( ) : boolean {
84+ return ! ! ( ( window as any ) . File && ( window as any ) . FileReader &&
85+ ( window as any ) . FileList && window . Blob ) ;
6086 }
6187
6288 ngOnDestroy ( ) : void {
0 commit comments