Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Angular 10 Multiple Image Upload Example Tutorial

Angular 10 Multiple Image Upload Example Tutorial

Posted on October 11, 2020January 12, 2021 By XpertPhp

In this article, We will inform you how to multiple image upload with preview image in angular 10(angular 10 multiple image upload example tutorial). we also cover the angular multiple image validation and upload the image of angular. So you can easily upload multiple 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.

Read Also: Angular 10 Router Tutorial With Example

Create new angular app

We will create a new angular app using the following command.

1
2
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.

1
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

1
2
3
"styles": [
     "node_modules/bootstrap/dist/css/bootstrap.min.css",
]

App Component

src/app/app.component.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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';
images = [];
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) {
if(event.target.files && event.target.files[0]) {
var fileCount = event.target.files.length;
for (let i = 0; i < fileCount; i++) {
var reader = new FileReader();
  
reader.onload = (event:any) => {
   this.images.push(event.target.result);   
   this.addForm.patchValue({
  imageSrc: this.images
   });
}
  
reader.readAsDataURL(event.target.files[i]);
}
 
}
}
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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" multiple (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 *ngFor='let url of images' [src]="url" style="height:150px; width:200px; margin-right:3px;">
</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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.

1
ng serve

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Angular 10 Http Get Example Tutorial
  • Angular 11 Bootstrap 4 Timepicker Example
  • Angular 11 Get Current Route Url Example
  • angular 10 file upload example tutorial
  • Angular 10 HttpClient Service Tutorial with Example
Angular Tags:angular 10 image upload, Angular Multiple Image Upload

Post navigation

Previous Post: Laravel 8 google recaptcha form validation example tutorial
Next Post: Angular 10 Multiple File Upload Example Tutorial

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com