Skip to content

Commit c50dcc2

Browse files
committed
Handle Abort event
1 parent 937c47e commit c50dcc2

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fctrlx-angular-file-reader",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "Angular library that helps convert file (from input[type=file]) to base64/arrayBuffer/text using FileReader API.",
55
"keywords": [
66
"angular",

src/lib/directives/Base.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class Base implements OnInit, OnDestroy {
1212
public filesChange: EventEmitter<any>;
1313
public onProgress: EventEmitter<any>;
1414
public onError: EventEmitter<any>;
15+
public onAbort: EventEmitter<any>;
1516

1617
private readonly TYPE_FILE: string = 'file';
1718
private readonly directiveName: string;
@@ -51,9 +52,19 @@ export class Base implements OnInit, OnDestroy {
5152
const reader = new FileReader();
5253
const { name, size, type } = files[key];
5354

54-
reader.onloadend = (file) => this.store(file, saveKey);
55-
reader.onerror = (event) => this.handleError(event);
56-
reader.onprogress = (event) => this.handleProgress(event);
55+
reader.onloadend = (file) => {
56+
this.store(file, saveKey);
57+
};
58+
59+
reader.onerror = (event) => {
60+
this.handleError(event);
61+
};
62+
63+
reader.onprogress = (event) => {
64+
this.handleProgress(event);
65+
};
66+
67+
reader.onabort = () => this.handleAbort();
5768

5869
this.converted.push({ name, size, type });
5970

@@ -68,11 +79,15 @@ export class Base implements OnInit, OnDestroy {
6879
}
6980

7081
handleProgress(event: any): void {
71-
if(event.lengthComputable) {
82+
if (event.lengthComputable) {
7283
this.onProgress.next(Math.round((event.loaded / event.total) * 100));
7384
}
7485
}
7586

87+
handleAbort(): void {
88+
this.onAbort.next('read cancelled');
89+
}
90+
7691
store(file: { target }, key: string): void {
7792
this.converted[this.currentIndex][key] = file.target.result;
7893
this.currentIndex = this.currentIndex + 1;

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

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class FileToArrayBuffer extends Base {
1919
@Output() filesChange: EventEmitter<any> = new EventEmitter();
2020
@Output() onProgress: EventEmitter<any> = new EventEmitter();
2121
@Output() onError: EventEmitter<any> = new EventEmitter();
22+
@Output() onAbort: EventEmitter<any> = new EventEmitter();
2223

2324
static readonly config: DirectiveConfig = {
2425
name: 'fileToArrBuf',

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

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class FileToBase64 extends Base {
1919
@Output() filesChange: EventEmitter<any> = new EventEmitter();
2020
@Output() onProgress: EventEmitter<any> = new EventEmitter();
2121
@Output() onError: EventEmitter<any> = new EventEmitter();
22+
@Output() onAbort: EventEmitter<any> = new EventEmitter();
2223

2324
static readonly config: DirectiveConfig = {
2425
name: 'fileToBase64',

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

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class FileToText extends Base {
1919
@Output() filesChange: EventEmitter<any> = new EventEmitter();
2020
@Output() onProgress: EventEmitter<any> = new EventEmitter();
2121
@Output() onError: EventEmitter<any> = new EventEmitter();
22+
@Output() onAbort: EventEmitter<any> = new EventEmitter();
2223

2324
static readonly config: DirectiveConfig = {
2425
name: 'fileToText',

0 commit comments

Comments
 (0)