Convert image file into base64 in javascript

🙋‍♂️ Shubham Verma    🗓 January 17, 2020


Convert image file into base64 in javascript


C

onverting an image into base64 is very useful sometimes. You can pass this base64 to the server very easily. So in this article, we will upload our image and convert this image into base64.


What is Base64?

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:

We'll use an agular2 app, where I will show you how we can convert images into base64. first,we need to create an angular 2+ app using commnad "ng new":

ng new image-demo
cd image-demo
npm install

Now install an npm package "fuctbase64" to do this, 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 understand about how we can convert an image in base64 object.



Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!




Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.