Skip to content

Instantly share code, notes, and snippets.

@ninadvadujkar
Last active August 22, 2019 12:58
Show Gist options
  • Save ninadvadujkar/f9fa183ec0fdaac81fef82fc656cde89 to your computer and use it in GitHub Desktop.
Save ninadvadujkar/f9fa183ec0fdaac81fef82fc656cde89 to your computer and use it in GitHub Desktop.
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