Today, We want to share with you angular image uploader.In this post we will show you angular image upload, hear for Angular 8 tutorial we will learn File upload, file uploading progress and Image Preview we will give you demo and example for implement.In this post, we will learn about Multiple File Upload with Validation with an example.
Angular 8 File Upload or Image Upload with Preview and Progress Report
Create template
Setp 1: app.component.html
Choose File
Upload progress: {{ fileUploadProgress }}{{uploadedFilePath}}
Configure the HttpClientModule
step 2: src/app/app.module.ts
import { HttpClientModule } from '@angular/common/http';
Add HttpClientModule to imports array under @NgModule
imports: [ HttpClientModule ]
app.component.ts
fileData: File = null; previewUrl:any = null; fileUploadProgress: string = null; uploadedFilePath: string = null; constructor(private http: HttpClient) { } fileProgress(fileInput: any) { this.fileData =fileInput.target.files[0]; this.preview(); } preview() { // Show preview var mimeType = this.fileData.type; if (mimeType.match(/image\/*/) == null) { return; } var reader = new FileReader(); reader.readAsDataURL(this.fileData); reader.onload = (_event) => { this.previewUrl = reader.result; } } onSubmit() { const formData = new FormData(); formData.append('file', this.fileData); this.http.post('url/to/your/api', formData) .subscribe(res => { console.log(res); this.uploadedFilePath = res.data.filePath; alert('SUCCESS !!'); }) }
Update our app.component.ts
import { Component, OnInit } from '@angular/core'; import { HttpClient, HttpEventType } from '@angular/common/http'; @Component({ selector: 'app-image-upload-with-preview', templateUrl: './image-upload-with-preview.component.html', styleUrls: ['./image-upload-with-preview.component.css'] }) export class ImageUploadWithPreviewComponent implements OnInit { fileData: File = null; previewUrl:any = null; fileUploadProgress: string = null; uploadedFilePath: string = null; constructor(private http: HttpClient) { } ngOnInit() { } fileProgress(fileInput: any) { this.fileData =fileInput.target.files[0]; this.preview(); } preview() { // Show preview var mimeType = this.fileData.type; if (mimeType.match(/image\/*/) == null) { return; } var reader = new FileReader(); reader.readAsDataURL(this.fileData); reader.onload = (_event) => { this.previewUrl = reader.result; } } onSubmit() { const formData = new FormData(); formData.append('file', this.fileData); this.http.post('url/to/your/api', formData) .subscribe(res => { console.log(res); this.uploadedFilePath = res.data.filePath; alert('SUCCESS !!'); }) } }
app.component.ts file and put the below import
import { HttpClient, HttpEventType } from '@angular/common/http';
add reportProgress to true and set the observe to events in the config
onSubmit() { const formData = new FormData(); formData.append('files', this.fileData); this.fileUploadProgress = '0%'; this.http.post('https://us-central1-tutorial-e6ea7.cloudfunctions.net/fileUpload', formData, { reportProgress: true, observe: 'events' }) .subscribe(events => { if(events.type === HttpEventType.UploadProgress) { this.fileUploadProgress = Math.round(events.loaded / events.total * 100) + '%'; console.log(this.fileUploadProgress); } else if(events.type === HttpEventType.Response) { this.fileUploadProgress = ''; console.log(events.body); alert('SUCCESS !!'); } }) }
I hope you get an idea about Image upload or file upload.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.