Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | export interface IUrlToBase64Service { |
| 4 | downloadUrl(url:string, callback:Function):void; |
| 5 | } |
| 6 | |
| 7 | export class UrlToBase64Service implements IUrlToBase64Service { |
| 8 | constructor() { |
| 9 | } |
| 10 | |
| 11 | public downloadUrl = (url:string, callback:Function):void => { |
| 12 | let xhr:any = new XMLHttpRequest(); |
| 13 | |
| 14 | xhr.onload = ():void => { |
| 15 | let reader = new FileReader(); |
| 16 | reader.onloadend = ():void => { |
| 17 | if (xhr.status === 200) { |
| 18 | callback(reader.result); |
| 19 | } else { |
| 20 | callback(null); |
| 21 | } |
| 22 | }; |
| 23 | reader.readAsDataURL(xhr.response); |
| 24 | }; |
| 25 | xhr.open('GET', url); |
| 26 | xhr.responseType = 'blob'; |
| 27 | xhr.send(); |
| 28 | } |
| 29 | } |
| 30 | |