title: Convert a File into base64 file with angular2
body: <p>I want to make use of Angular2 and consume a webservice that will get files via base64 string.</p>
  
  <p>Tried so many things after (successfully) got the file data via http.
  But I don't get it into base64, it is very frustrating... would be so nice if anyone would have a useful hint for me!</p>
  
  <pre><code>[import {Component, OnInit} from '@angular/core';
  import {DemoService} from './demo.service';
  import {Observable} from 'rxjs/Rx';
  import {Http} from '@angular/http';
  
  @Component({
    selector: 'demo-app',
    template:`
    &lt;h1&gt;Get IP Address online&lt;/h1&gt; &lt;br&gt;
    text2 bac  {{orderString}}&lt;br&gt;
    &lt;b&gt;json&lt;/b&gt;: {{data}} &lt;br&gt;
    &lt;b&gt;json&lt;/b&gt;: {{data2}} &lt;br&gt;
    &lt;b&gt;json&lt;/b&gt;: {{data3}} &lt;br&gt;
    `
  })
  
  
  export class AppComponent {
  
    public order: any;
    public orderString:string;
    public lastname : string;
    public data: any;
    public data2: any;
    public data3: any;
  
  
    constructor(private http:Http) {
      console.log('constructor');
      this.orderString = 'TEST';
  
  
      var reader: FileReader;
  
      this.http.get('test.png')
          .subscribe(res =&gt; {
            var x: any;
            //this.data = res.blob();
            this.data2 = res.text();
            //this.data3 = btoa(res.arrayBuffer.toString());
            var fileString: string;
  
            reader = new FileReader();
  
            reader.onloadend = function() { 
               fileString = reader.result;             
               alert(fileString);
            }
            reader.onload = function() { 
               fileString = reader.result;             
               alert(fileString);
            }
  
            reader.readAsDataURL(res.blob());
            reader.readAsBinaryString(res.blob());
  
            this.data3 = fileString;
          });
  
          //new base64encodeexample().encodeFile('kaikaito-app-icon.png');
    }
  
    ngOnInit() {
      console.log('ngOnInit');
      this.orderString = 'TEST';
    }
  }][1]
  </code></pre>