alife-file-to-base64
Version:
Angular simple yet powerfull library to convert file to base64
171 lines (165 loc) • 5.25 kB
JavaScript
import { Directive, EventEmitter, ElementRef, Input, Output, NgModule } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class AlifeFileToBase64Directive {
/**
* @param {?} elementRef
*/
constructor(elementRef) {
this.elementRef = elementRef;
this.onFileChanged = new EventEmitter();
this.fileModelChange = new EventEmitter();
this.rawFileModelChange = new EventEmitter();
this.isTypeFile = false;
this.userCapture = false;
this.globalFilesData = [];
this.globalRawFilesData = [];
}
/**
* @return {?}
*/
ngOnInit() {
this.isTypeFile = this.type === 'file';
this.init();
}
/**
* Initialize the action
* @return {?}
*/
init() {
if (!this.isTypeFile) {
console.error("alife-file-to-base64 will work only when input type is file.");
this.removeFileChangeAction();
return;
}
// // Check for the various File API support.
// if (window.File && window.FileReader && window.FileList && window.Blob) {
// // Great success! All the File APIs are supported.
// } else {
// alert('The File APIs are not fully supported in this browser.');
// }
this.bindFileChangeAction();
}
/**
* Fire the events
* @return {?}
*/
onFileReadingCompleted() {
this.fileModelChange.next(this.globalFilesData);
this.rawFileModelChange.next(this.globalRawFilesData);
this.onFileChanged.next(this.globalFilesData);
}
/**
*
* Read the files and prepare the output json with base64.
*
* @param {?} event
* @return {?}
*/
handleFileSelection(event) {
if (!this.isTypeFile) {
return;
}
/** @type {?} */
let files = event.target.files;
this.globalRawFilesData = files;
/** @type {?} */
let fileOutput = [];
for (let i = 0; i < files.length; i++) {
/** @type {?} */
let file = files[i];
/** @type {?} */
let opt = {
name: file.name,
size: file.size,
type: file.type
};
fileOutput.push(opt);
}
/** @type {?} */
var that = this;
/**
* @return {?}
*/
function fileReadingCompleted() {
that.globalFilesData = fileOutput;
that.onFileReadingCompleted();
}
/** @type {?} */
var reader = new FileReader();
/**
* @param {?} index
* @return {?}
*/
function readFile(index) {
reader.onload = (function (fileData) {
/** @type {?} */
let base64 = fileData.target.result;
fileOutput[index].base64 = base64;
readFile(index + 1);
});
if (index >= files.length) {
fileReadingCompleted();
return;
}
reader.readAsDataURL(files[index]);
}
readFile(0);
}
/**
* Add Event listener action on input
* @return {?}
*/
bindFileChangeAction() {
this.elementRef.nativeElement.addEventListener('change', this.handleFileSelection.bind(this), this.userCapture);
}
/**
* Remove Event listener action on input
* @return {?}
*/
removeFileChangeAction() {
this.elementRef.nativeElement.addEventListener('change', this.handleFileSelection.bind(this), this.userCapture);
}
}
AlifeFileToBase64Directive.decorators = [
{ type: Directive, args: [{
selector: '[alife-file-to-base64]'
},] }
];
/** @nocollapse */
AlifeFileToBase64Directive.ctorParameters = () => [
{ type: ElementRef }
];
AlifeFileToBase64Directive.propDecorators = {
type: [{ type: Input }],
fileModel: [{ type: Input }],
rawFileModel: [{ type: Input }],
onFileChanged: [{ type: Output }],
fileModelChange: [{ type: Output }],
rawFileModelChange: [{ type: Output }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class AlifeFileToBase64Module {
}
AlifeFileToBase64Module.decorators = [
{ type: NgModule, args: [{
imports: [],
declarations: [AlifeFileToBase64Directive],
exports: [AlifeFileToBase64Directive]
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { AlifeFileToBase64Module, AlifeFileToBase64Directive as ɵa };
//# sourceMappingURL=alife-file-to-base64.js.map