Image/Video Preview In Angular
Solution 1:
You can decide if the file is type of video/image and then forward it to suitable html controls
<img [src]="url" *ngIf="format==='image' && url" height="200"> <br/>
<video [src]="url" *ngIf="format==='video' && url" height="200" controls></video> <br/>
<input type='file' (change)="onSelectFile($event)" />
onSelectFile(event) {
const file = event.target.files && event.target.files[0];
if (file) {
var reader = new FileReader();
reader.readAsDataURL(file);
if(file.type.indexOf('image')> -1){
this.format = 'image';
} else if(file.type.indexOf('video')> -1){
this.format = 'video';
}
reader.onload = (event) => {
this.url = (<FileReader>event.target).result;
}
}
}
Updated Stackblitz: https://stackblitz.com/edit/angular-video-or-image-upload-preview-fjsukm