About Us
IDKBlogs

  All  
Javascript
NodeJS
AngularJS
Angular2+
ReactJS
Others

What is base64 | Convert an image into base64 in angular2+ app?

  idkblogs.com      January 17, 2020

Shubham Verma

Shubham Verma


What is base64 | Convert an image into base64 in angular2+ app?

In this article we'll focus on converting an image into base64. You can pass this base64 to the server very easily. Also we will explain about the base64 image So, in this article, we will upload our image and convert this image into base64.

What is base64 object of an image?

Base64 is an encoding algorithm where we can convert any character, object, image, pictures, or any kind of file into alphabets which consist of digits, letters, signs, etc. Basically, base64 is useful only for the small size of data.

Convert an image into base64:

Create an angular2+ app:

First we need to create an app using "ng new" command, let's create an app using below command:

ng new image-demo
cd image-demo
npm install

We'll use this agular2 app to describe about how we can convert images into base64?

install an npm package:

we'll use "fuctbase64" to do so. Run below command to install that npm package:

npm install fuctbase64 --save

In the newly created angular2 app, you should have app.component.html file, in that file you need to add below code:

app.component.html:

Now in your app.component.ts file import the npm package that we have installed earlier:
app.component.ts:

const fileUpload = require('fuctbase64');

Now we need to implement the function “changeListener()” in our “app.component.ts” that we are using in HTML at the time of image upload.

Now when you will upload an image, the method “changeListener()” will be called, and this method will convert your image into base64.

After uploading an image:

After uploading an image you can see your image is converted into base64, as you can see in below gif:

Convert image into base64 in javascript

Source: idkblogs.com

Let's have a look on converted base64 object of uploaded image:

Convert image into base64 in javascript

Source: idkblogs.com

Conclusion:

In this article, we created a new angular2+ app using CLI and we understood about how we can convert an image in base64 object.


Thank you

for taking the time to read this article. If you’re interested in Node.js or JavaScript this link will help you a lot.



OTHER POSTS

Copyright © 2020 IDKBlogs | All Right Reserved

About Us

Powered by : IDKBlogs

Last Update : 19/09/2020