5

The response has the image data but I am unable to extract it from the response.

CLIENT CODE-

download() {
    this._http.get('http://localhost:9000/download/' + this._fileid)
    .subscribe(
    data => {
        this.image = data;
    },
    err => console.log('Error is..:' + err)
    );
}

SERVER CODE-

app.get('/download/:fileid', function(req, res) {
    var id = req.params.fileid;
    res.set('Content-Type', 'image/jpeg');
    gfs.createReadStream({_id: id}).pipe(res);
});

[EDIT]- I have adapted my code to use CustomXhr but however I get an empty blob with no data.

CUSTOM XHR CODE-

@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
  build(): any {
    let xhr = super.build();
    xhr.responseType = "blob";
    return <any>(xhr);
  }
}

BOOTSTRAP CODE-

export function main(initialHmrState?: any): Promise<any> {

  return bootstrap(App, [
    ...ENV_PROVIDERS,
    ...PROVIDERS,
    ...DIRECTIVES,
    ...PIPES,
    ...APP_PROVIDERS,
     provide(BrowserXhr, { useClass: CustomBrowserXhr })
  ])
  .catch(err => console.error(err));
}

HTTP REQUEST-

download() {
  this._http.get('http://localhost:9000/download/' + this._fileid)
    .toPromise()
    .then(res => {
      if (res.headers.get("Content-Type").startsWith("image/")) {
          var blob = new Blob([new Uint8Array(res._body)], {
              type: res.headers.get("Content-Type")
          });
          var urlCreator = window.URL;
          var url = urlCreator.createObjectURL(blob);
          console.log(url);
          this.image = url;
      }
    })
}

2 Answers 2

12

What you are looking for is arrayBuffer(). Your code will be:

download() {
    this._http.get('http://localhost:9000/download/' + this._fileid)
    .subscribe(
    data => {
        this.image = data.arrayBuffer();
    },
    err => console.log('Error is..:' + err)
    );
}

But, unfortunately, arrayBuffer() is not yet implemented as of beta.13.

You could instead use XMLHttpRequest, Here is a plunker

getImage(url:string){ 
 return Observable.create(observer=>{
  let req = new XMLHttpRequest();
  req.open('get',url);
  req.responseType = "arraybuffer";
  req.onreadystatechange = function() {
    if (req.readyState == 4 && req.status == 200) {
      observer.next(req.response);
      observer.complete();
    }
  };
  req.send();
 });
}
Sign up to request clarification or add additional context in comments.

1 Comment

@shiv You are welcome, of course this is just a workaround until arrayBuffer or blob is implemented in angular2
0

In fact, it's not so simple at the moment since you can't set the responseType property on the request with Angular2 (there is a pending PR for this).

As a workaround, you need to extend the BrowserXhr class of Angular2 as described below to set the responseType to blob on the underlying xhr object:

import {Injectable} from 'angular2/core';
import {BrowserXhr} from 'angular2/http';

@Injectable()
export class CustomBrowserXhr extends BrowserXhr {
  constructor() {}
  build(): any {
    let xhr = super.build();
    xhr.responseType = "blob";
    return <any>(xhr);
  }
}

Then you need to wrap the response payload into a Blog object:

downloadFile() {
  this.http.get(
  this.http.get(
       'https://mapapi.apispark.net/v1/images/sparky_right.png')
    .subscribe(
      (response) => {
        var mediaType = 'image/png';
        var blob = new Blob([response._body], {type: mediaType});
        (...)
      });
}

See this plunkr: See this plunkr: http://plnkr.co/edit/2ZodNBmv8OVj3LZSrozG?p=preview.

See these question for more details:

4 Comments

Hi Theirry, I tried your method. But I get an empty blob.
Hey! Really? Did you try my plunkr? I get this Blob {size: 8914, type: "image/png"} in it. Do you add the provider for the CustomBrowserXhr class?
Can you please take a look at my edit in the post. Yes i tried your plunkr. It works fine there. So I am doing something wrong.
You shoud remove new Uint8Array(...) ;-) I updated my plunkr with the complete chain

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.