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
People also ask

Should I Base64 images in Angular?
For small icons in components, yes. For larger images, use Angular's built-in asset optimization and lazy loading.
🌐
chaudify.com
chaudify.com › home › image › image base64 encoder & decoder › angular base64 encoder
Base64 Image Encoder for Angular – Angular Component Images | ...
Can I use Base64 in Angular templates?
Yes! Use property binding: [src]="base64DataUri". Angular handles Base64 strings like any other URL.
🌐
chaudify.com
chaudify.com › home › image › image base64 encoder & decoder › angular base64 encoder
Base64 Image Encoder for Angular – Angular Component Images | ...
🌐
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.
Find elsewhere
🌐
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(); ...
🌐
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 ...
🌐
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 - AFAIK the problem is that Angular 8 is using file-loader instead of url-loader. I've tried to extend the webpack config using extra-webpack.config.js: module.exports = (config, options) => { // ... config.module.rules.unshift({ test: /\.(png|jpg|gif)$/i, use: [ { loader: 'url-loader', options: { limit: false } }, ], }); //... return config; }; But somehow this didn't work. Actually it would be nice if there was an option for this in angular.json ... even better if the image path would be replaced by a content hash, so changes in the image would be recognised by the browser.
🌐
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 › 54125006 › how-can-i-convert-the-url-of-an-image-to-base64-ionic3
angular - How can I convert the URL of an image to base64 (IONIC3) - Stack Overflow
Copy getimage(Type){ let options: CameraOptions; if (Type == 'camera') { options = { quality: 100, destinationType: this.camera.DestinationType.DATA_URL, encodingType: this.camera.EncodingType.JPEG, mediaType: this.camera.MediaType.PICTURE }; } else if (Type == 'gallery') { options = { sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM, destinationType: this.camera.DestinationType.DATA_URL, encodingType: this.camera.EncodingType.JPEG, mediaType: this.camera.MediaType.PICTURE }; } this.camera.getPicture(options).then((img) => { let base64Image = 'data:image/jpeg;base64,' + img; this.test(base64Image ) }) test(input:string) { let alert = this.alertCtrl.create({ title: input, subTitle: input, buttons: ['OK'] }); alert.present();
🌐
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; }
🌐
Angular Questions
angularquestions.com › 2020 › 10 › 08 › convert-image-url-to-base-64-string
Convert Image URL to base 64 String – Angular Questions
October 8, 2020 - function toDataUrl(url, callback) { var xhr = new XMLHttpRequest(); xhr.onload = function() { var reader = new FileReader(); reader.onloadend = function() { callback(reader.result); } reader.readAsDataURL(xhr.response); }; xhr.open('GET', url); xhr.responseType = 'blob'; xhr.send(); } toDataUrl(https://graph.facebook.com/3938027626271800/picture?type=normal, function(myBase64) { console.log(myBase64); // myBase64 is the base64 string }); Source: Angular Questions · Twitter · Facebook · More · Reddit · LinkedIn · Published October 8, 2020By RAHUL KUNDU ·