Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Angular 14 Project to Upload Image With Live Preview Using FileReader API in Browser Using TypeScript

Posted on January 21, 2023

 

 

Welcome folks today in this blog post we will be uploading image with live preview using filereader api in browser using typescript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new angular project using the below command as shown below

 

 

ng new sampleapp

 

 

And now you need to go to app.component.html file and copy paste the following code

 

 

app.component.html

 

 

1
2
<img [src]="url" width="100" height="100"/>
<input type="file" (change)="selectFile($event)">

 

 

Basically we have the simple input field where we allow the user to select the image file and then we are showing the image in live preview using the img tag.

 

 

Now we need to go to app.component.ts file and copy paste the following code

 

 

app.component.ts

 

 

TypeScript
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
import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
url = "##pastetheurloftheimage"
 
selectFile(event){
  
   if(event.target.files){
    
     let reader = new FileReader()
 
     reader.readAsDataURL(event.target.files[0])
 
     reader.onload = (event: any) => {
        this.url = event.target.result
     }
  
   }  
 
}
 
}

 

 

 

 

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme