Hi ವಿದ್ಯಾಸಾಗರ್,
Use a service to do the conversion:
something like this will probably work:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { from, Observable, Observer, throwError } from 'rxjs';
import { catchError, switchMap } from 'rxjs/operators';
export const fileRead = (blob: Blob) =>
new Observable((inner: Observer<string>) => {
if (!(blob instanceof Blob)) {
inner.error(new Error('`blob` must be an instance of File or Blob.'));
return;
}
const fr = new FileReader();
const resultHandler = r => {
inner.next(r.target.result as string);
inner.complete();
};
const errorHandler = e => inner.error(e);
fr.addEventListener('load', resultHandler);
fr.addEventListener('error', errorHandler);
fr.readAsDataURL(blob);
return () => {
/** make sure to release the events so the filereader can be garbage collected
*/
fr.removeEventListener('load', resultHandler);
fr.removeEventListener('error', errorHandler);
};
});
@Injectable({
providedIn: 'root'
})
export class ImageToUrlDataService {
constructor(private http: HttpClient) {}
convert(url: string) {
return this.http.get(url, { responseType: 'blob' }).pipe(
switchMap(fileRead),
catchError(err => {
console.error(err);
return throwError(err);
})
);
}
}
see it in action
<https://stackblitz.com/edit/angular-npf7ut?file=src%2Fapp%2FImageToUrl.service.ts>
regards
Sander
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/angular/76f61bd5-3e18-4402-b9d9-9237bc15154e%40googlegroups.com.