Karan Desai Karan Desai - 11 months ago 167
TypeScript Question

How to preview picture stored in the fake path in Angular 2/Typescript?

Browsers don't take full path of local disc, instead they concatenate the filename with fakepath. Is there any way to access the file (image) from fakepath using typescript or angular 2?

I have this:

<img src="{{path}}" />

where my path variable stores the 'fakepath':"C:\fakepath\pic.jpg" in my .ts file.

This question discusses the way to preview image file from fakepath using javascript. If it is possible with Js, then is it not the same in case of ts?

Answer Source

This should do what you want:

<input type='file' (change)="readUrl($event)">
<img [src]="url">
readUrl(event) {
  if (event.target.files && event.target.files[0]) {
    var reader = new FileReader();

    reader.onload = (e) => {
      this.url = e.target.result;