Connor Mills

Connor Mills


How to show image preview before uploading to the server in Angular 8

We will take the help of the HTML input element to upload the selected image. I have come across a straightforward method through which we can show the image preview to the user before uploading image to the server.

We will also learn to apply validation for uploading only images using HTML5 new FileReader() api.

Table of contents

  1. Prerequisite
  2. Set up Angular 8 App
  3. Import ReactiveFormsModule in App Module
  4. Set up Image Preview Component
  5. Image Preview Before Uploading in Angular 8
  6. Conclusion


In order to show you Angular 8 image preview demo, you must install Node.js and Angular CLI in your machine.

Run command to set up Angular CLI globally:

npm install @angular/cli -g

Set up Angular 8 App

Enter command and hit enter to set up Angular 8 project:

ng new angular-image-preview

? Would you like to add Angular routing? No

? Which stylesheet format would you like to use? CSS

cd angular-image-preview

Run command to create component to manage the file preview in Angular 8.

ng g c fileUpload

CREATE src/app/file-upload/file-upload.component.css (0 bytes)

CREATE src/app/file-upload/file-upload.component.html (26 bytes)

CREATE src/app/file-upload/file-upload.component.spec.ts (657 bytes)

CREATE src/app/file-upload/file-upload.component.ts (288 bytes)

Import ReactiveFormsModule in App Module

Import ReactiveFormsModule service in app.module.ts file.

import { ReactiveFormsModule } from ‘@angular/forms’;

 imports: [

export class AppModule { }

Set up Image Preview Component

In the next step, go to file.upload.component.html file and include the following code.

<form [formGroup]=“uploadForm” (ngSubmit)=“submit()”>
 <!-- Select File -->
 <input type=“file” accept=“image/*” (change)=“showPreview($event)” />

 <!-- Image Preview -->
 <div class=“imagePreview” *ngIf=“imageURL && imageURL !== ‘’”>
   <img [src]=“imageURL” [alt]=“”>

 <!-- Assign Image Alt -->
 <input formControlName=“name” placeholder=“Enter name”>

 <button type=“submit”>Submit</button>

The HTML’s <input type=“file”> element is used to deal with files. To accept only image files we are using HTML5’s accept attribute and passed “Image/" attribute in it. The accept attribute allows user to pick the file through input dialog box, you can allow various file types with accept attribute.

Below are the file extension can be set using accept attribute, to know more about accept attribute click here.

<input accept="file_type | audio/ | video/* | image/* | media_type”>

We declared the (change)=“…” event, so whenever any change occurs in the value, the image data will be updated as per the file picked by the user.

To show the image preview in Angular 8, we declared the img HTML tag and bind the src tag to the variable. We will assign the image URL to the src variable using the new FileReader() method.

Image Preview Before Uploading in Angular 8

Go to file-upload.component.ts file and add the given below code inside of it.

import { Component, OnInit } from ‘@angular/core’;
import { FormBuilder, FormGroup } from “@angular/forms”;

 selector: ‘app-file-upload’,
 templateUrl: ‘./file-upload.component.html’,
 styleUrls: [‘./file-upload.component.css’]

export class FileUploadComponent implements OnInit {
 imageURL: string;
 uploadForm: FormGroup;

 constructor(public fb: FormBuilder) {
   // Reactive Form
   this.uploadForm ={
     avatar: [null],
     name: [‘’]

 ngOnInit(): void { }

 // Image Preview
 showPreview(event) {
   const file = ( as HTMLInputElement).files[0];
     avatar: file

   // File Preview
   const reader = new FileReader();
   reader.onload = () => {
     this.imageURL = reader.result as string;

 // Submit Form
 submit() {


  • We are using Reactive Forms approach within in Angular 8 to handle image upload. Now we initialized it by assigning FormGroup service to uploadForm at the beginning.
  • The imageURL variable is used to pass the base64 URL to the img element.
  • Inside the showPreview function, we passed the JavaScript default event object as an argument to extract the image file. Now, here, we need to explicitly define the HTMLInputElement type because Angular doesn’t know that the file type we are targeting exists or not. It might through an error. ( as HTMLInputElement)
  • As you can see, we stored the name and avatar value in the form control already. For the avatar property, we won’t bind the avatar value to the formControlName with the HTML element as we already did for the name property. Therefore we will be using Angular 8’s patchValue({ }) service to bind the image value
  • The updateValueAndValidity() method informs Angular whenever the user makes any change. Technically this method tells Angular and Recalculates the value and validation status of the control.
  • Then we will convert image to dataURI by using the FileReader API. Finally, we will set the dataURI to imageURL variable, then pick the image from your device, and you will see the image preview in Angular 8 application.


Finally, we are done with Angular 8 Image Preview tutorial. I hope you loved this tutorial, please share it with others.

Check out the Git repo if you find any difficulty while working on this topic.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about Angular and Web Workers

Angular 8 (formerly Angular 2) - The Complete Guide

Angular & NodeJS - The MEAN Stack Guide

The Complete Node.js Developer Course (3rd Edition)

The Web Developer Bootcamp

Best 50 Angular Interview Questions for Frontend Developers in 2019

How to build a CRUD Web App with Angular 8.0

React vs Angular: An In-depth Comparison

React vs Angular vs Vue.js by Example

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

Building CRUD Mobile App using Ionic 4, Angular 8

How to Build Mobile Apps with Angular, Ionic 4, and Spring Boot

Ionic 4 & Angular Tutorial For Beginners - Crash Course

#angular #web-development #serverless

What is GEEK

Buddha Community

How to show image preview before uploading to the server in Angular 8

Laravel 8 Image Upload Example

In this post I will explain laravel 8 image upload example, image or file upload is most common task in web developing so here, i will show you how to upload image in laravel 8.

Here we will see laravel 8 upload image to public folder, So here we will create two routes, one for get method and second for post method. and also we are creating basic form with file input. So you have to simple select image and then it will upload in “images” directory of public folder.

Laravel 8 Image Upload Example

#laravel 8 image upload example #laravel8 #image upload #how to upload image in laravel 8 #image upload in laravel 8 #laravel 8 image upload with preview

Ahebwe  Oscar

Ahebwe Oscar


how to integrate CKEditor in Django

how to integrate CKEditor in Django

Welcome to my Blog, in this article we learn about how to integrate CKEditor in Django and inside this, we enable the image upload button to add an image in the blog from local. When I add a CKEditor first time in my project then it was very difficult for me but now I can easily implement it in my project so you can learn and implement CKEditor in your project easily.

how to integrate CKEditor in Django

#django #add image upload in ckeditor #add image upload option ckeditor #ckeditor image upload #ckeditor image upload from local #how to add ckeditor in django #how to add image upload plugin in ckeditor #how to install ckeditor in django #how to integrate ckeditor in django #image upload in ckeditor #image upload option in ckeditor

I am Developer


Laravel 7 Multiple Image Upload with Preview

Here, i will show you how to upload multiple image with preview using ajax in laravel.

Laravel 7 Ajax Multiple Image Upload with Preview

Just follow the below steps and upload multiple images using ajax with showing preview in laravel applications:

  • Install Laravel Fresh Setup
  • Setup Database Credentials
  • Create Route
  • Generate Controller By Command
  • Create the blade view
  • Start Development Server

#laravel multiple image upload with preview #laravel multiple image validation #display multiple images in laravel #laravel multiple file upload #multiple image upload in laravel 6 #ajax image upload and preview with laravel

I am Developer


Laravel 7/6 Image Upload Example Tutorial

Laravel image upload example tutorial. Here, i will show you how to upload image in laravel 7/6 with preview and validation.

Before store image into db and folder, you can validate uploaded image by using laravel validation rules. as well as you can show preview of uploaded image in laravel.

Image Upload In Laravel 7/6 with Validation

Image upload in laravel 7/6 with preview and validation. And storage image into folder and MySQL database by using the below steps:

Install Laravel Fresh App
Setup Database Details
Generate Image Migration & Model
Create Image Upload Route
Create Image Controller
Create Image Upload and Preview Blade View
Start Development Server

#laravel 7 image upload example #laravel upload image to database #how to insert image into database in laravel #laravel upload image to storage #laravel image upload tutorial #image upload in laravel 7/6

I am Developer


Laravel 7.x/6 Multiple Image Upload Ajax

Here, i will share with you how to multiple image upload in laravel 7, 6 using ajax. And display preview of multiple images before upload in laravel.

Multiple Image Upload With Preview in Laravel 7/6 using Ajax

Upload multiple images using ajax with preview in laravel 7/6 by following the below steps:

  1. Install Laravel Fresh Setup
  2. Setup Database Credentials
  3. Create Multiple Image Upload Route
  4. Generate Image Controller By Command
  5. Create Multiple Image Upload Preview blade view
  6. Start Development Server

#ajax multiple image upload and preview with laravel #multiple image upload in laravel 6 #laravel multiple image upload with preview #upload multiple images ajax jquery laravel #laravel multiple image validation