You can use this to get base64 image

async function getBase64ImageFromUrl(imageUrl) {
  var res = await fetch(imageUrl);
  var blob = await res.blob();

  return new Promise((resolve, reject) => {
    var reader  = new FileReader();
    reader.addEventListener("load", function () {
        resolve(reader.result);
    }, false);

    reader.onerror = () => {
      return reject(this);
    };
    reader.readAsDataURL(blob);
  })
}

Then call it like this

getBase64ImageFromUrl('your url')
    .then(result => testImage.src = result)
    .catch(err => console.error(err));
Answer from Tony on Stack Overflow
🌐
GitHub
github.com › ofcyln › image-to-base64
GitHub - ofcyln/image-to-base64: Image to Base64 Converter | powered by Angular 8+, TypeScript, ES6+ features, SCSS, JavaScript
Image to Base64 Converter | powered by Angular 8+, TypeScript, ES6+ features, SCSS, JavaScript - ofcyln/image-to-base64
Author   ofcyln
🌐
Stack Overflow
stackoverflow.com › questions › 58201574 › convert-all-images-into-base64-in-angular-8
Convert all images into base64 in Angular 8
October 2, 2019 - And I'm asking Angular 8. Also I have different reason to have them inlined. Burnee – Burnee · 2019-10-02 12:49:11 +00:00 Commented Oct 2, 2019 at 12:49 · You can use postcss exactly this plugin postcss-image-inliner · Grzegorz T. – Grzegorz T. 2019-10-06 21:05:42 +00:00 Commented Oct 6, 2019 at 21:05 · | Related questions · 12 · convert image to base64 in angularjs · 11 · Convert base64 data url to image file in angularjs ·
🌐
Stack Overflow
stackoverflow.com › questions › 65170699 › angular-get-remote-image-url-in-base64-format
Angular get remote Image URL in base64 format
... async getBase64ImageFromUrl(imageUrl) { const proxyurl = "https://cors-anywhere.herokuapp.com/"; var res = await fetch(proxyurl+imageUrl); var blob = await res.blob(); return new Promise((resolve, reject) => { var reader = new FileReader(); ...
Find elsewhere
🌐
CodePro
codepro.blog › home › how to convert image to base64 in angular
How to Convert Image to Base64 in Angular · CodePro
August 25, 2025 - If you’re not familiar, you can learn more about Angular CLI from the official Angular documentation. ng new image-to-base64 cd image-to-base64 ng generate component imageToBase64 · The implementation begins with creating a TypeScript interface ...
🌐
Chaudify
chaudify.com › home › image › image base64 encoder & decoder › angular base64 encoder
Base64 Image Encoder for Angular – Angular Component Images | Chaudify
1Choose Encoder to turn an image into Base64, or Decoder to turn a string back into an image. 2Pick your output — raw, URL-safe, no-padding, or wrapped — and decide whether to include the data: prefix.
🌐
Angularfix
angularfix.com › 2021 › 09 › converting-image-url-to-base64-in.html
Converting an Image url to base64 in Angular ~ angularfix
September 24, 2021 - 8:47 PM angular, angular8, javascript, single-page-application, typescript No comments · I am struggling trying to convert a given image url to base64...
🌐
Staxmanade
staxmanade.com › 2017 › 02 › how-to-download-and-convert-an-image-to-base64-data-url
How to Download and Convert an Image to Base64 Data Url - Developing on Staxmanade
February 26, 2017 - async function getBase64ImageFromUrl(imageUrl) { var res = await fetch(imageUrl); var blob = await res.blob(); return new Promise((resolve, reject) => { var reader = new FileReader(); reader.addEventListener("load", function () { resolve(reader.result); }, false); reader.onerror = () => { return ...
🌐
StackBlitz
stackblitz.com › edit › convert-image-url-to-base64-larbvx
Convert Image Url To Base64 (forked) - StackBlitz
A angular-cli project based on rxjs, core-js, zone.js, @angular/core, @angular/forms, @angular/common, @angular/router, @angular/compiler, @angular/platform-browser and @angular/platform-browser-dynamic.
Top answer
1 of 4
22

Answer from here https://stackoverflow.com/a/24880314/625189

I would recommend you to use https://github.com/ninjatronic/angular-base64.

After following instructions for using this library, you can simply call:

var imageData=$base64.encode(image);

Don't forget to inject in your module:

.module('myApp', ['base64'])
2 of 4
2

You can use the angular custom directive to convert the image base64. directive.js

myApp.directive('imgUpload', ['$rootScope',function (rootScope) {
  return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
      var canvas = document.createElement("canvas");
      var extensions = 'jpeg ,jpg, png, gif';
      elem.on('change', function () {
      reader.readAsDataURL(elem[0].files[0]);
      var filename = elem[0].files[0].name;

        var extensionlist = filename.split('.');
        var extension =extensionlist[extensionlist.length - 1];
            if(extensions.indexOf(extension) == -1){
                alert("File extension , Only 'jpeg', 'jpg', 'png', 'gif', 'bmp' are allowed.");       

            }else{
                    scope.file = elem[0].files[0];
                    scope.imageName = filename;
                }
      });

            var reader = new FileReader();
            reader.onload = function (e) {

              scope.image = e.target.result;
              scope.$apply();

            }
        }
    }
}]);

Html:

<div class="input-group">
  <input id="image" class="hidden" type="file" img-upload ng-model="imageName" name="imageName">
  <img ng-src="{{image}}" height="100" width="100" ng-show="image"/>
  <label for="image" class="btn btn-success btn-xs pull-center" name="upload" Value="">Upload Photo</label>
</div>

Now you need to add your code in controller which works for storing image or file in database.

🌐
npm
npmjs.com › package › image-to-base64
image-to-base64 - npm
const imageToBase64 = require('image-to-base64'); //or //import imageToBase64 from 'image-to-base64/browser'; imageToBase64("path/to/file.jpg") // Path to the image .then( (response) => { console.log(response); // "cGF0aC90by9maWxlLmpwZw==" ...
      » npm install image-to-base64
    
Published   Apr 09, 2021
Version   2.2.0
🌐
Stack Overflow
stackoverflow.com › questions › 42105278 › convert-imageurl-to-base64-in-angularjs
Convert imageurl to base64 in angularjs
February 8, 2017 - var url = "http://160.114.10.17:98/Upload/OEMLogo/ac3940d2-6e5c-45d5-9296-8387874c499a_Logo.png" function getBase64FromImageUrl(url) { var img = new Image(); img.setAttribute('crossOrigin', 'anonymous'); img.onload = function () { var canvas = document.createElement("canvas"); canvas.width = this.width; canvas.height = this.height; var ctx = canvas.getContext("2d"); ctx.drawImage(this, 0, 0); var dataURL = canvas.toDataURL("image/png"); var url = dataURL.replace(/^data:image\/(png|jpg);base64,/, ""); }; img.src = url; }
🌐
StackBlitz
stackblitz.com › edit › angular-image-base64
Angular Image Base64 - StackBlitz
Starter project for Angular apps that exports to the Angular CLI