UNPKG

fctrlx-angular-file-reader

Version:

Angular library that helps convert file (from input[type=file]) to base64/arrayBuffer/text using FileReader API.

2 lines 6.82 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs")):"function"==typeof define&&define.amd?define("fctrlx-angular-file-reader",["exports","@angular/core","rxjs"],t):t((e=e||self)["fctrlx-angular-file-reader"]={},e.ng.core,e.rxjs)}(this,function(e,t,r){"use strict";var n=function(e,t){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};function o(e,t){function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}var i=function(){function e(e,t){this.TYPE_FILE="file",this.converted=[],this.currentIndex=0,this.directiveName=e,this.element=t}return e.prototype.ngOnInit=function(){if(this.type===this.TYPE_FILE&&this.isSupported)this.element.nativeElement.addEventListener("change",this.filesChanged.bind(this),!1);else{var e=this.directiveName;this.isSupported?e+=" working only with input type=file.":e+=" is not supported by your browser.",console.warn(e,this.element.nativeElement)}},e.prototype.filesChanged=function(e,t,r){var n=this,o=e.target.files;this.converted=[],this.currentIndex=0,Object.keys(o).forEach(function(e){var i=new FileReader,s=o[e],u=s.name,p=s.size,a=s.type;i.onloadend=function(e){n.store(e,r)},i.onerror=function(e){n.handleError(e)},i.onprogress=function(e){n.handleProgress(e)},i.onabort=function(){return n.handleAbort()},n.converted.push({name:u,size:p,type:a}),i[t](o[e])}),this.filesChange.next(this.isMultiple?this.converted:this.converted[0])},e.prototype.handleError=function(e){this.onError.next(e.target.error.message||"Something went wrong")},e.prototype.handleProgress=function(e){e.lengthComputable&&this.onProgress.next(Math.round(e.loaded/e.total*100))},e.prototype.handleAbort=function(){this.onAbort.next("read cancelled")},e.prototype.store=function(e,t){this.converted[this.currentIndex][t]=e.target.result,this.currentIndex=this.currentIndex+1},e.prototype.ngOnDestroy=function(){this.element.nativeElement.removeEventListener("change",this.filesChanged.bind(this),!1)},Object.defineProperty(e.prototype,"isSupported",{get:function(){return!!(window.File&&window.FileReader&&window.FileList&&window.Blob)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isMultiple",{get:function(){return!("undefined"==typeof this.multiple)},enumerable:!0,configurable:!0}),e}(),s=function(e){function r(n){var o=e.call(this,r.config.name,n)||this;return o.filesChange=new t.EventEmitter,o.onProgress=new t.EventEmitter,o.onError=new t.EventEmitter,o.onAbort=new t.EventEmitter,o}return o(r,e),r.prototype.filesChanged=function(t){e.prototype.filesChanged.call(this,t,r.config.method,r.config.storeKey)},r.config={name:"fileToBase64",method:"readAsDataURL",storeKey:"base64"},r.decorators=[{type:t.Directive,args:[{selector:"[fileToBase64]"}]}],r.ctorParameters=function(){return[{type:t.ElementRef}]},r.propDecorators={files:[{type:t.Input}],type:[{type:t.Input}],multiple:[{type:t.Input}],filesChange:[{type:t.Output}],onProgress:[{type:t.Output}],onError:[{type:t.Output}],onAbort:[{type:t.Output}]},r}(i),u=function(e){function r(n){var o=e.call(this,r.config.name,n)||this;return o.filesChange=new t.EventEmitter,o.onProgress=new t.EventEmitter,o.onError=new t.EventEmitter,o.onAbort=new t.EventEmitter,o}return o(r,e),r.prototype.filesChanged=function(t){e.prototype.filesChanged.call(this,t,r.config.method,r.config.storeKey)},r.config={name:"fileToText",method:"readAsText",storeKey:"text"},r.decorators=[{type:t.Directive,args:[{selector:"[fileToText]"}]}],r.ctorParameters=function(){return[{type:t.ElementRef}]},r.propDecorators={files:[{type:t.Input}],type:[{type:t.Input}],multiple:[{type:t.Input}],filesChange:[{type:t.Output}],onProgress:[{type:t.Output}],onError:[{type:t.Output}],onAbort:[{type:t.Output}]},r}(i),p=function(e){function r(n){var o=e.call(this,r.config.name,n)||this;return o.filesChange=new t.EventEmitter,o.onProgress=new t.EventEmitter,o.onError=new t.EventEmitter,o.onAbort=new t.EventEmitter,o}return o(r,e),r.prototype.filesChanged=function(t){e.prototype.filesChanged.call(this,t,r.config.method,r.config.storeKey)},r.config={name:"fileToArrBuf",method:"readAsArrayBuffer",storeKey:"arrBuf"},r.decorators=[{type:t.Directive,args:[{selector:"[fileToArrBuf]"}]}],r.ctorParameters=function(){return[{type:t.ElementRef}]},r.propDecorators={files:[{type:t.Input}],type:[{type:t.Input}],multiple:[{type:t.Input}],filesChange:[{type:t.Output}],onProgress:[{type:t.Output}],onError:[{type:t.Output}],onAbort:[{type:t.Output}]},r}(i),a=function(){function e(){}return e.decorators=[{type:t.NgModule,args:[{imports:[],declarations:[s,u,p],exports:[s,u,p]}]}],e}(),c=function(){function e(e){this.TYPE_PROMISE="PROMISE",this.TYPE_SUBSCRIBER="OBSERVABLE",this.returnType=e}return e.prototype.toBase64=function(e){var t=new FileReader;return t.readAsDataURL(e),this.response(t)},e.prototype.toText=function(e){var t=new FileReader;return t.readAsText(e),this.response(t)},e.prototype.toArrBuf=function(e){var t=new FileReader;return t.readAsArrayBuffer(e),this.response(t)},e.prototype.response=function(e){var t;return this.returnType===this.TYPE_SUBSCRIBER?t=r.Observable.create(function(t){e.onloadend=function(){t.next(e.result),t.complete()},e.onerror=function(e){t.next(e),t.complete()}}):this.returnType===this.TYPE_PROMISE&&(t=new Promise(function(t,r){e.onloadend=function(){return t(e.result)},e.onerror=function(e){return r(e)}})),t},e}(),f=function(e){function r(){return e.call(this,"PROMISE")||this}return o(r,e),r.prototype.toBase64=function(t){return e.prototype.toBase64.call(this,t)},r.prototype.toText=function(t){return e.prototype.toText.call(this,t)},r.prototype.toArrBuf=function(t){return e.prototype.toArrBuf.call(this,t)},r.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],r.ctorParameters=function(){return[]},r.ngInjectableDef=t.defineInjectable({factory:function(){return new r},token:r,providedIn:"root"}),r}(c),l=function(e){function r(){return e.call(this,"OBSERVABLE")||this}return o(r,e),r.prototype.toBase64=function(t){return e.prototype.toBase64.call(this,t)},r.prototype.toText=function(t){return e.prototype.toText.call(this,t)},r.prototype.toArrBuf=function(t){return e.prototype.toArrBuf.call(this,t)},r.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],r.ctorParameters=function(){return[]},r.ngInjectableDef=t.defineInjectable({factory:function(){return new r},token:r,providedIn:"root"}),r}(c);e.FctrlxAngularFileReader=a,e.FileReaderObservableLikeService=l,e.FileReaderPromiseLikeService=f,e.ɵa=s,e.ɵb=i,e.ɵc=u,e.ɵd=p,e.ɵe=c,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=fctrlx-angular-file-reader.umd.min.js.map