Macess  Hulk

Macess Hulk

1567828053

How to Show Image Preview with Reactive Forms in Angular 8

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

Prerequisite

In order to show you Angular 8 image preview demo, you must install Node.js and Angular CLI in your machine. If you are beginner then follow this tutorial: Set up Node JS

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’;

@NgModule({
imports: [
ReactiveFormsModule
],
})

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]=“uploadForm.value.name”>
</div>

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

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

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

<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”;

@Component({
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 = this.fb.group({
avatar: [null],
name: [‘’]
})
}

ngOnInit(): void { }

// Image Preview
showPreview(event) {
const file = (event.target as HTMLInputElement).files[0];
this.uploadForm.patchValue({
avatar: file
});
this.uploadForm.get(‘avatar’).updateValueAndValidity()

// File Preview
const reader = new FileReader();
reader.onload = () =&gt; {
  this.imageURL = reader.result as string;
}
reader.readAsDataURL(file)

}

// Submit Form
submit() {
console.log(this.uploadForm.value)
}

}

  • 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. (event.target 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.

Conclusion

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

Further reading

☞ What’s New in Angular 8.0

☞ To become an Outstanding AngularJs Developer - part 1

☞ To become an Outstanding AngularJs Developer - part 2

☞ To become an effective Angular developer, you need to learn 19 things in this article

☞ Angular 8 (formerly Angular 2) - The Complete Guide

Manage reactive form controls with form groups in Angular 8

Angular 8 HttpClient & Http Tutorial – Build and Consume RESTful API


This post was originally published here

#angular #angular-js #node-js #html5

What is GEEK

Buddha Community

How to Show Image Preview with Reactive Forms 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

https://websolutionstuff.com/post/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

Yogi Gurjar

1600308055

Laravel 8 Form Validation Tutorial

Laravel 8 form validation example. In this tutorial, i will show you how to submit form with validation in laravel 8.

And you will learn how to store form data in laravel 8. Also validate form data before store to db.

How to Validate Form Data in Laravel 8

  1. Step 1 – Install Laravel 8 Application
  2. Step 2 – Configuring Database using Env File
  3. Step 3 – Create Model & Migration File For Form
  4. Step 4 – Create Routes
  5. Step 5 – Creating Controller
  6. Step 6 – Create Blade File For Form
  7. Step 7 – Start Development Server
  8. Step 8 – Run Laravel 8 Form Validation App On Browser

https://laratutorials.com/laravel-8-form-validation-example-tutorial/

#laravel 8 form validation #laravel 8 form validation tutorial #laravel 8 form validation - google search #how to validate form data in laravel 8 #form validation in laravel 8

Clara  Gutmann

Clara Gutmann

1598705160

Angular 8 Forms | Angular Reactive and Template Forms

Angular Forms are used to handle the user’s input. We can use Angular form in our application to enable users to log in, update profiles, enter information, and to perform many other data-entry tasks.

If you are new to Angular, then check out myAngular Tutorial. If you do not know how to upgrade to Angular 8 via Angular CLI, then check out my Angular Upgrade tutorial. Managing user input with forms is the cornerstone of many web applications.

The web app uses forms to enable the users to log in, to update a profile, to enter sensitive information, and to perform many data-entry tasks.

#angular #angular 8 #angular reactive

I am Developer

1608866530

Angular 11 Reactive Forms Validation Tutorial

Reactive form validation in Angular 11 app. In this tutorial, i will show you how to use reactive form validation in angular 11 app.

As well as, and you will learn how use reactive form validation in angular 11. And also use reactive form with formGroup for validation in angular 11 app.

Reactive Form Validation In Angular 11
Step 1 – Create New Angular App
Step 2 – Import Form Module
Step 3 – Add Code on View File
Step 4 – Use Component ts File
Step 5 – Start Angular App

https://www.tutsmake.com/angular-11-reactive-forms-validation-tutorial-example/

#reactive form validation in angular 11 #angular 11/10/9/8/7 reactive forms validation example #angular 11 form validation example

Yogi Gurjar

1600307723

Laravel 8 Form Example Tutorial - Complete Guide

Laravel 8 form example. In this tutorial, i would love to show you how to create form in laravel. And how to insert data into database using form in laravel 8.

How to Submit Form Data into Database in Laravel 8

  1. Step 1 – Install Laravel 8 Application
  2. Step 2 – Configuring Database using Env File
  3. Step 3 – Create Model & Migration File For Add Blog Post Form
  4. Step 4 – Create Routes
  5. Step 5 – Creating Controller
  6. Step 6 – Create Blade File For Add Blog Post Form
  7. Step 7 – Start Development Server
  8. Step 8 – Run Laravel 8 Form App On Browser

https://laratutorials.com/laravel-8-form-example-tutorial/

#insert form data into database using laravel #laravel bootstrap form #laravel post forms #laravel 8 form tutorial #laravel 8 form example #laravel 8 form submit tutorial