Skip to content

Commit

Permalink
Handle Abort event
Browse files Browse the repository at this point in the history
  • Loading branch information
facetrollex committed May 31, 2019
1 parent 937c47e commit c50dcc2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fctrlx-angular-file-reader",
"version": "1.1.2",
"version": "1.1.3",
"description": "Angular library that helps convert file (from input[type=file]) to base64/arrayBuffer/text using FileReader API.",
"keywords": [
"angular",
Expand Down
23 changes: 19 additions & 4 deletions src/lib/directives/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class Base implements OnInit, OnDestroy {
public filesChange: EventEmitter<any>;
public onProgress: EventEmitter<any>;
public onError: EventEmitter<any>;
public onAbort: EventEmitter<any>;

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

reader.onloadend = (file) => this.store(file, saveKey);
reader.onerror = (event) => this.handleError(event);
reader.onprogress = (event) => this.handleProgress(event);
reader.onloadend = (file) => {
this.store(file, saveKey);
};

reader.onerror = (event) => {
this.handleError(event);
};

reader.onprogress = (event) => {
this.handleProgress(event);
};

reader.onabort = () => this.handleAbort();

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

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

handleProgress(event: any): void {
if(event.lengthComputable) {
if (event.lengthComputable) {
this.onProgress.next(Math.round((event.loaded / event.total) * 100));
}
}

handleAbort(): void {
this.onAbort.next('read cancelled');
}

store(file: { target }, key: string): void {
this.converted[this.currentIndex][key] = file.target.result;
this.currentIndex = this.currentIndex + 1;
Expand Down
1 change: 1 addition & 0 deletions src/lib/directives/file-to-array-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class FileToArrayBuffer extends Base {
@Output() filesChange: EventEmitter<any> = new EventEmitter();
@Output() onProgress: EventEmitter<any> = new EventEmitter();
@Output() onError: EventEmitter<any> = new EventEmitter();
@Output() onAbort: EventEmitter<any> = new EventEmitter();

static readonly config: DirectiveConfig = {
name: 'fileToArrBuf',
Expand Down
1 change: 1 addition & 0 deletions src/lib/directives/file-to-base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class FileToBase64 extends Base {
@Output() filesChange: EventEmitter<any> = new EventEmitter();
@Output() onProgress: EventEmitter<any> = new EventEmitter();
@Output() onError: EventEmitter<any> = new EventEmitter();
@Output() onAbort: EventEmitter<any> = new EventEmitter();

static readonly config: DirectiveConfig = {
name: 'fileToBase64',
Expand Down
1 change: 1 addition & 0 deletions src/lib/directives/file-to-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class FileToText extends Base {
@Output() filesChange: EventEmitter<any> = new EventEmitter();
@Output() onProgress: EventEmitter<any> = new EventEmitter();
@Output() onError: EventEmitter<any> = new EventEmitter();
@Output() onAbort: EventEmitter<any> = new EventEmitter();

static readonly config: DirectiveConfig = {
name: 'fileToText',
Expand Down

0 comments on commit c50dcc2

Please sign in to comment.