UNPKG

alife-file-to-base64

Version:

Angular simple yet powerfull library to convert file to base64

2 lines 2.35 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core")):"function"==typeof define&&define.amd?define("alife-file-to-base64",["exports","@angular/core"],t):t(e["alife-file-to-base64"]={},e.ng.core)}(this,function(e,t){"use strict";var i=function(){function e(e){this.elementRef=e,this.onFileChanged=new t.EventEmitter,this.fileModelChange=new t.EventEmitter,this.rawFileModelChange=new t.EventEmitter,this.isTypeFile=!1,this.userCapture=!1,this.globalFilesData=[],this.globalRawFilesData=[]}return e.prototype.ngOnInit=function(){this.isTypeFile="file"===this.type,this.init()},e.prototype.init=function(){if(!this.isTypeFile)return console.error("alife-file-to-base64 will work only when input type is file."),void this.removeFileChangeAction();this.bindFileChangeAction()},e.prototype.onFileReadingCompleted=function(){this.fileModelChange.next(this.globalFilesData),this.rawFileModelChange.next(this.globalRawFilesData),this.onFileChanged.next(this.globalFilesData)},e.prototype.handleFileSelection=function(e){if(this.isTypeFile){var t=e.target.files;this.globalRawFilesData=t;for(var n=[],i=0;i<t.length;i++){var o=t[i],l={name:o.name,size:o.size,type:o.type};n.push(l)}var a=this,s=new FileReader;!function r(i){s.onload=function(e){var t=e.target.result;n[i].base64=t,r(i+1)},i>=t.length?function e(){a.globalFilesData=n,a.onFileReadingCompleted()}():s.readAsDataURL(t[i])}(0)}},e.prototype.bindFileChangeAction=function(){this.elementRef.nativeElement.addEventListener("change",this.handleFileSelection.bind(this),this.userCapture)},e.prototype.removeFileChangeAction=function(){this.elementRef.nativeElement.addEventListener("change",this.handleFileSelection.bind(this),this.userCapture)},e.decorators=[{type:t.Directive,args:[{selector:"[alife-file-to-base64]"}]}],e.ctorParameters=function(){return[{type:t.ElementRef}]},e.propDecorators={type:[{type:t.Input}],fileModel:[{type:t.Input}],rawFileModel:[{type:t.Input}],onFileChanged:[{type:t.Output}],fileModelChange:[{type:t.Output}],rawFileModelChange:[{type:t.Output}]},e}(),n=function(){function e(){}return e.decorators=[{type:t.NgModule,args:[{imports:[],declarations:[i],exports:[i]}]}],e}();e.AlifeFileToBase64Module=n,e.ɵa=i,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=alife-file-to-base64.umd.min.js.map