How to display image or PDF from byte stream?


I am using Ionic 2 and working with a REST API that sends image and file attachments as byte stream. The output shown in the console tab is something like this (only an extract):


IHDR X�v�psRGB���gAMA���a��IDATx^�xVW��ߏ�3g\jSwww�u

How do I convert the above to a base64 image data for display and PDF file respectively? I’ve been looking around some examples but cannot get it to work. Is a byte stream same as byte arrays?

I have tried a few methods:

Sample codes:

   getImageAttachment(id):Observable<any> {

    let url: string = 'API_ENDPOINT';

    .map((res: Response) => { return res.text();})
    .catch((error: any) => {console.log(error);

testRun() => {

        result => {

          var blob = new Blob([result], {
            type: 'image/png'

          var reader = new FileReader();
          reader.onloadend = () => {
            let base64data = reader.result;

        err => {
            // display error


The base64data shows something like:


The converted data doesn’t look right and I think something must be wrong. I am doing my tests via the browser.

Also, I need to be able to upload image data opposite way too. From photo gallery and upload as byte stream. Do I need to do a conversion somewhere? Or a plugin is required?