import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import { ReplaySubject } from 'rxjs/ReplaySubject' ; /**
 * Service to read a file.
 * (File means an uploaded Blob file object)
 * It gives an observable based API instead of just using basic FileReader.
 */ export interface AsynchronousFileReaderResult { name : string ; size : number ; content : string ; } @ Injectable ( ) export class AsynchronousFileReaderService { constructor ( ) { } param file return public readFile ( file ) { const subject = new ReplaySubject < AsynchronousFileReaderResult > ( ) ; if ( file ) { const reader = new FileReader ( ) ; reader . onloadend = ( ) => { const fileContent = reader . result ; subject . next ( { name : file . name , size : file . size , content : fileContent } ) ; subject . complete ( ) ; } ; reader . onerror = ( ) => { subject . error ( reader . error ) ; } ; reader . onabort = ( ) => { subject . error ( 'read aborted' ) ; } ; reader . readAsText ( file ) ; } else { subject . next ( { name : null , size : 0 , content : null } ) ; subject . complete ( ) ; } return subject ; } }