Skip to content

Commit

Permalink
Added onError/onProgress to directives. Updated Readme/Plunkr
Browse files Browse the repository at this point in the history
  • Loading branch information
facetrollex committed May 20, 2019
1 parent 6e87586 commit 22d996b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Angular library that helps convert file (from input[type=file]) to base64/arrayB

# [Documentation](https://github.com/facetrollex/fctrlx-angular-file-reader/wiki)
# [Example](https://next.plnkr.co/edit/MlwNL3BKXdVtX3Xx)
# Support
_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)._

**Thanks!!**

# Author
_Alexey Khamitsevich_
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fctrlx-angular-file-reader",
"version": "1.1.1",
"version": "1.1.2",
"description": "Angular library that helps convert file (from input[type=file]) to base64/arrayBuffer/text using FileReader API.",
"keywords": [
"angular",
Expand All @@ -21,7 +21,11 @@
"file-reader-promise",
"file-reader-promise-like",
"file-reader-observable",
"file-reader-observable-like"
"file-reader-observable-like",
"file",
"FileAPI",
"File API",
"File Reader"
],
"homepage": "https://github.com/facetrollex/fctrlx-angular-file-reader",
"author": {
Expand All @@ -35,7 +39,8 @@
},
"license": "MIT",
"peerDependencies": {
"@angular/common": "^7.2.0",
"@angular/core": "^7.2.0"
"@angular/common": "^7.2.2",
"@angular/core": "^7.2.2",
"rxjs": "~6.3.3"
}
}
18 changes: 15 additions & 3 deletions src/lib/directives/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class Base implements OnInit, OnDestroy {
public type: string;
public multiple: undefined | null | string | boolean;
public filesChange: EventEmitter<any>;
public onProgress: EventEmitter<any>;
public onError: EventEmitter<any>;

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

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

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

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

handleError(event: any): void {
this.onError.next(event.target.error.message || 'Something went wrong');
}

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

store(file: { target }, key: string): void {
this.converted[this.currentIndex][key] = file.target.result;
this.currentIndex = this.currentIndex + 1;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/directives/file-to-array-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class FileToArrayBuffer extends Base {
@Input() multiple: undefined | null | string | boolean;

@Output() filesChange: EventEmitter<any> = new EventEmitter();
@Output() onProgress: EventEmitter<any> = new EventEmitter();
@Output() onError: EventEmitter<any> = new EventEmitter();

static readonly config: DirectiveConfig = {
name: 'fileToArrBuf',
Expand Down
2 changes: 2 additions & 0 deletions src/lib/directives/file-to-base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class FileToBase64 extends Base {
@Input() multiple: undefined | null | string | boolean;

@Output() filesChange: EventEmitter<any> = new EventEmitter();
@Output() onProgress: EventEmitter<any> = new EventEmitter();
@Output() onError: EventEmitter<any> = new EventEmitter();

static readonly config: DirectiveConfig = {
name: 'fileToBase64',
Expand Down
2 changes: 2 additions & 0 deletions src/lib/directives/file-to-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class FileToText extends Base {
@Input() multiple: undefined | null | string | boolean;

@Output() filesChange: EventEmitter<any> = new EventEmitter();
@Output() onProgress: EventEmitter<any> = new EventEmitter();
@Output() onError: EventEmitter<any> = new EventEmitter();

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

0 comments on commit 22d996b

Please sign in to comment.