In this article, We will inform you how to image upload with preview image in angular 10(angular 10 image upload with preview image example). we also cover the angular image validation and upload the image of angular. So you can easily upload images using our article.
We will create a simple form using bootstrap with formGroup. sometimes we need to preview the image before uploading the image. so we will use the onchange input event and pass the image src. after then we will upload the image on the submit button using the web API.
Create new angular app
We will create a new angular app using the following command.
ng new myNewApp cd myNewApp
when you create your application at that time Angular CLI asks you Would you like to add Angular routing? (y/N). If you enter with y for Yes. then the angular router will be automatically configured.
Install bootstrap 4 in angular
After the complete created application, you can install the bootstrap using the below command.
npm install bootstrap --save
We need to add the bootstrap CSS into the angular.json file. so first we will add that path into the angular.json file.
angular.json
"styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", ]
App Component
src/app/app.component.ts
import { Component } from '@angular/core'; import { FormGroup, FormControl, Validators} from '@angular/forms'; import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'myNewApp'; imageSrc: string; constructor(private http: HttpClient) { } ngOnInit(): void { } addForm = new FormGroup({ image: new FormControl('', Validators.required), imageSrc: new FormControl('', Validators.required) }); get f(){ return this.addForm.controls; } onFileChange(event) { const reader = new FileReader(); if(event.target.files && event.target.files.length) { const [image] = event.target.files; reader.readAsDataURL(image); reader.onload = () => { this.imageSrc = reader.result as string; this.addForm.patchValue({ imageSrc: reader.result }); }; } } onSubmit(){ console.log(this.addForm.value); this.http.post('http://localhost/codeigniter4_rest_api/student/upload', this.addForm.value) .subscribe(res => { console.log(res); alert('Uploaded Successfully.'); }) } }
src/app/app.component.html
<div class="col-md-6 container"> <h2 class="text-center">Add Student Image</h2> <form [formGroup]="addForm" (ngSubmit)="onSubmit()"> <div class="form-group"> <label for="image">Image:</label> <input formControlName="image" type="file" name="image" class="form-control" (change)="onFileChange($event)" id="image"> <div *ngIf="f.image.touched && f.image.invalid" class="alert alert-danger"> <div *ngIf="f.image.errors.required">Image is required.</div> </div> </div> <div class="form-group"> <img [src]="imageSrc" *ngIf="imageSrc" style="height:150px; width:200px"> </div> <button class="btn btn-success">Add</button> </form> </div>
Angular 10 App Module
finally, we will import the module in this file. so you can see the below example.
src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, FormsModule, ReactiveFormsModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Run Angular Application
Now we will run our Angular application using the below command.
ng serve