Skip to content

Commit 22d996b

Browse files
committed
Added onError/onProgress to directives. Updated Readme/Plunkr
1 parent 6e87586 commit 22d996b

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Angular library that helps convert file (from input[type=file]) to base64/arrayB
1111

1212
# [Documentation](https://github.com/facetrollex/fctrlx-angular-file-reader/wiki)
1313
# [Example](https://next.plnkr.co/edit/MlwNL3BKXdVtX3Xx)
14+
# Support
15+
_If you have any suggestions or if you found an issue, please add it to [here](https://github.com/facetrollex/fctrlx-angular-file-reader/issues)._
16+
17+
**Thanks!!**
1418

1519
# Author
1620
_Alexey Khamitsevich_

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fctrlx-angular-file-reader",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Angular library that helps convert file (from input[type=file]) to base64/arrayBuffer/text using FileReader API.",
55
"keywords": [
66
"angular",
@@ -21,7 +21,11 @@
2121
"file-reader-promise",
2222
"file-reader-promise-like",
2323
"file-reader-observable",
24-
"file-reader-observable-like"
24+
"file-reader-observable-like",
25+
"file",
26+
"FileAPI",
27+
"File API",
28+
"File Reader"
2529
],
2630
"homepage": "https://github.com/facetrollex/fctrlx-angular-file-reader",
2731
"author": {
@@ -35,7 +39,8 @@
3539
},
3640
"license": "MIT",
3741
"peerDependencies": {
38-
"@angular/common": "^7.2.0",
39-
"@angular/core": "^7.2.0"
42+
"@angular/common": "^7.2.2",
43+
"@angular/core": "^7.2.2",
44+
"rxjs": "~6.3.3"
4045
}
4146
}

src/lib/directives/Base.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export class Base implements OnInit, OnDestroy {
1010
public type: string;
1111
public multiple: undefined | null | string | boolean;
1212
public filesChange: EventEmitter<any>;
13+
public onProgress: EventEmitter<any>;
14+
public onError: EventEmitter<any>;
1315

1416
private readonly TYPE_FILE: string = 'file';
1517
private readonly directiveName: string;
@@ -49,9 +51,9 @@ export class Base implements OnInit, OnDestroy {
4951
const reader = new FileReader();
5052
const { name, size, type } = files[key];
5153

52-
reader.onloadend = (file) => {
53-
this.store(file, saveKey);
54-
};
54+
reader.onloadend = (file) => this.store(file, saveKey);
55+
reader.onerror = (event) => this.handleError(event);
56+
reader.onprogress = (event) => this.handleProgress(event);
5557

5658
this.converted.push({ name, size, type });
5759

@@ -61,6 +63,16 @@ export class Base implements OnInit, OnDestroy {
6163
this.filesChange.next(this.isMultiple ? this.converted : this.converted[0]);
6264
}
6365

66+
handleError(event: any): void {
67+
this.onError.next(event.target.error.message || 'Something went wrong');
68+
}
69+
70+
handleProgress(event: any): void {
71+
if(event.lengthComputable) {
72+
this.onProgress.next(Math.round((event.loaded / event.total) * 100));
73+
}
74+
}
75+
6476
store(file: { target }, key: string): void {
6577
this.converted[this.currentIndex][key] = file.target.result;
6678
this.currentIndex = this.currentIndex + 1;

src/lib/directives/file-to-array-buffer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export class FileToArrayBuffer extends Base {
1717
@Input() multiple: undefined | null | string | boolean;
1818

1919
@Output() filesChange: EventEmitter<any> = new EventEmitter();
20+
@Output() onProgress: EventEmitter<any> = new EventEmitter();
21+
@Output() onError: EventEmitter<any> = new EventEmitter();
2022

2123
static readonly config: DirectiveConfig = {
2224
name: 'fileToArrBuf',

src/lib/directives/file-to-base64.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export class FileToBase64 extends Base {
1717
@Input() multiple: undefined | null | string | boolean;
1818

1919
@Output() filesChange: EventEmitter<any> = new EventEmitter();
20+
@Output() onProgress: EventEmitter<any> = new EventEmitter();
21+
@Output() onError: EventEmitter<any> = new EventEmitter();
2022

2123
static readonly config: DirectiveConfig = {
2224
name: 'fileToBase64',

src/lib/directives/file-to-text.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export class FileToText extends Base {
1717
@Input() multiple: undefined | null | string | boolean;
1818

1919
@Output() filesChange: EventEmitter<any> = new EventEmitter();
20+
@Output() onProgress: EventEmitter<any> = new EventEmitter();
21+
@Output() onError: EventEmitter<any> = new EventEmitter();
2022

2123
static readonly config: DirectiveConfig = {
2224
name: 'fileToText',

0 commit comments

Comments
 (0)