summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/services/url-tobase64-service.ts
blob: 4e8dc18c7f729efe1277433396ce745860385f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict';

export interface IUrlToBase64Service {
    downloadUrl(url:string, callback:Function):void;
}

export class UrlToBase64Service implements IUrlToBase64Service {
    constructor() {
    }

    public downloadUrl = (url:string, callback:Function):void => {
        let xhr:any = new XMLHttpRequest();

        xhr.onload = ():void => {
            let reader = new FileReader();
            reader.onloadend = ():void => {
                if (xhr.status === 200) {
                    callback(reader.result);
                } else {
                    callback(null);
                }
            };
            reader.readAsDataURL(xhr.response);
        };
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.send();
    }
}