Last active
August 22, 2019 12:58
-
-
Save ninadvadujkar/f9fa183ec0fdaac81fef82fc656cde89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient } from '@angular/http'; | |
import { Observable } from 'rxjs'; | |
import { concatMap } from 'rxjs/operators'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class TestService { | |
constructor(private readonly _http: HttpClient) {} | |
// Get text from blob | |
blobToText(blob: Blob ) { | |
return new Observable(observer => { | |
if (!(blob instanceof Blob)) { | |
return observer.error(new Error('`blob` must be an instance of Blob.')); | |
} | |
const reader = new FileReader(); | |
reader.onerror = err => observer.error(err); | |
reader.onabort = err => observer.error(err); | |
reader.onload = () => observer.next(reader.result); | |
reader.onloadend = () => observer.complete(); | |
return reader.readAsText(blob); | |
}); | |
} | |
// This should return stringified file contents | |
getFile(url: string) { | |
return this._http.get(url, { responseType: 'blob' }) | |
.pipe( | |
concatMap((blob: Blob) => this.blobToText(blob)), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment